laravel如何将数据从表单传递到控制器
Posted
技术标签:
【中文标题】laravel如何将数据从表单传递到控制器【英文标题】:laravel how to pass the data from form to controller 【发布时间】:2021-12-11 08:07:00 【问题描述】:我正在尝试将数据从 html 表单传递到控制器,但是当我使用 dd 函数时,它没有显示来自 HTML 的任何输入值,它正在使用具有相同控制器的另一个 HTML 页面 我的控制器
public function carEdit(Request $request, Cars $cars, $id)
$data = $request->input();
dd($data); //when use dd it's give me
// array:1 [▼
// "_token" => "UE36YHG6TF1HnGsAkLFJAOFsrACZ8pmL3Ya9iFHE"
//]
// I think it should show me the input values in the form like this:
// array:1 [▼
// 'title'
// 'content'
// 'youtube'
// ...etc]
$cars = DB::table('cars')->where('id',$id)->get();
$car = Cars::findOrFail($id);
// $car->title = $data['title'];
// $car->content = $data['content'];
// $car->youtubevid = $data['youtubevid'];
// $car->bannerimage = $data['bannerimage'];
// $car->extrainfo = $data['extrainfo'];
// $car->location = $data['location'];
// $car->car_price = $data['car_price'];
$car->update();
return view('/dashboard/editCars')->with('cars',$cars);
HTML 代码
@extends('dashboard.base')
@section('content')
<div class="container-fluid">
<div class="fade-in">
<div class="card">
<div class="card-header"> edit car</div>
<div class="card-body">
<div class="body">
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">title</th>
<th scope="col">content</th>
<th scope="col">youtube</th>
<th scope="col">banner image</th>
<th scope="col">extra info</th>
<th scope="col">location</th>
<th scope="col">pricing</th>
</tr>
</thead>
<tbody>
-- dd($cars) --
@if (count($cars) != 0)
@foreach ($cars as $cars)
<tr>
<th scope="row">#</th>
<th > $cars->title </th>
<td> $cars->content </td>
<td> $cars->youtubevid </td>
<td> $cars->bannerimage </td>
<td> $cars->extrainfo </td>
<td> $cars->location </td>
<td> $cars->car_price </td>
</tr>
@endforeach
@endif
</tbody>
<thead>
<tr>
<form method="post" action="/cars/id/edit" enctype="multipart/form-data">
@csrf
<th scope="col">#</th>
<th scope="col"><input type="text" id="title"name="title" size="6" placeholder="new title"></input></th>
<th scope="col"><input type="text" name="content" size="6" placeholder="new content"></input></th>
<th scope="col"><input type="text" name="youtube" size="6" placeholder="new youtube"></input></th>
<th scope="col"><input type="text" name="bannerimage" size="6" placeholder="new banner image"></input></th>
<th scope="col"><input type="text" name="extrainfo" size="6" placeholder="new extra info"></input></th>
<th scope="col"><input type="text" name="location" size="6" placeholder="new location"></input></th>
<th scope="col"><input type="text" name="pricing" size="6" placeholder="new pricing"></input></th>
</form>
</tr>
</thead>
</table>
</div>
<button class="btn btn-danger" type="submit" onclick="location.href='/cars'">update</button>
</div>
</div>
</div>
</div>
@endsection
@section('javascript')
<script src=" asset('js/colors.js') "></script>
@endsection
【问题讨论】:
您声明了多个表单,并且您的表格布局有 2 个 thead 元素。您的提交按钮不知道要提交哪个表单。 【参考方案1】:您将 Id 作为静态传递。 我认为最好的方法就是你应该这样做
<form method="post" action="/cars/$variableYouWantToPassHere/edit"
enctype="multipart/form-data" >
public function carEdit(Request $request, $id)
$data = $request->all();
dd($data);//when use dd it's give me
然后在你的路线中
Route::POST('/cars/id/edit','YOURCONTROLLER@carEdit');
【讨论】:
以上是关于laravel如何将数据从表单传递到控制器的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Laravel 中将变量从视图传递到控制器而不使用 URL 或表单
如何在laravel php中将变量从视图传递到控制器而没有表单