描述了如何将数据从表单传递到控制器



我试图将数据从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

您将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 
}

then in your route

Route::POST('/cars/{id}/edit','YOURCONTROLLER@carEdit');

最新更新