当我单击“编辑按钮”时,我试图编辑记录未使用laravel记录到输入字段中
有人知道如何解决吗?
我面临错误
属性[product_name]在此集合实例上不存在。 (查看:/home/sigmadel/public_html/resources/views/admin/product_update.blade.php)
Controller
public function update_projects($products_id){
$category=DB::table('category')->get();
$projects=DB::table('projects')->where('products_id','=',$products_id)->get();
return view('admin/product_update',compact('category','projects'));
}
html视图
<form action="{{route('product.action')}}" method="post" >
@csrf
<div class="group-form">
<select class="form-control">
<option > Update Category</option>
@foreach($category as $categories)
<option value="">{{$categories->category_name}}</option>
@endforeach
</select>
</div>
<br>
<div class="group-form">
<input type="text" class="form-control" value="{{$projects->product_name}}"
placeholder="Update Product Name" name="product_name" >
</div>
<br>
<div class="group-form">
<input type="file" class="form-control" name="select_file" >
</div>
<br>
<div class="group-form">
<input type="submit" class=" btn btn-primary form-control" value="UPDATE" name="Update" >
</div>
</form>
<a class="btn btn-success" href="{{route('product.update',$projects->products_id)}}">Edit</a>
路线
Route::get('view_projects','AdminController@view_projects');
Route::get('product_update/{products_id}/edit','AdminController@update_projects')-
>name('product.update');
您一直做错了。您正在使用get()
时获得收集,但尝试将其用作对象。这样做。
路线:
Route::get('project_edit/{id}/edit','AdminController@edit_project')-
>name('project.edit');
Route::post('project_update/{id}/update','AdminController@update_project')-
>name('project.update');
控制器编辑功能:
public function edit_project($id){
$categories=DB::table('category')->get();
$project=DB::table('projects')->where('products_id','=',$id)->first();
return view('admin/product_update',compact('categories','project'));
}
编辑刀片:
<form action="{{route('project.update', $project->id)}}" method="POST">
@csrf
<div class="group-form">
<select class="form-control">
<option value="">Select Category</option>
@foreach($categories as $category)
<option value="{{$category->id}}" @if($project->category_id) == $category->id) selected @endif>{{$category->category_name}}</option>
@endforeach
</select>
</div>
<br>
<div class="group-form">
<input type="text" class="form-control" value="{{$project->product_name}}"
placeholder="Update Product Name" name="product_name" >
</div>
<br>
<div class="group-form">
<input type="file" class="form-control" name="select_file" >
</div>
<br>
<div class="group-form">
<input type="submit" class=" btn btn-primary form-control" value="UPDATE" name="Update" >
</div>
</form>
最后是控制器中的更新功能:
public function update_project(Request $request, $id){
//validation and update process
}
这里项目和产品混合不好,所以请根据您的结构进行更改。