[英]Edit and Update functions in Laravel 5.8
I am getting an error which says "Property [id] does not exist on this collection instance. (View: C:\newXampp\htdocs\testUser\resources\views\Stock\edit.blade.php)"我收到一条错误消息,提示“此集合实例上不存在属性 [id]。(查看:C:\newXampp\htdocs\testUser\resources\views\Stock\edit.blade.php)”
This is my Controller这是我的 Controller
public function editStock(Stock $id)
{
//
$Stock = Stock::find($id);
return view('Stock.edit', compact('Stock', 'id'));
// return response($stock);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Stock $stock
* @return \Illuminate\Http\Response
*/
public function updateStock(Request $request, $id)
{
$request->validate([
'id'=>'required',
'stock_name'=>'required',
'stock_qty'=>'required',
'stock_unit'=>'required',
'stock_price_per_kg'=>'required',
'stock_weight_per_qty'=>'required'
]);
$stock = Stock::find($id);
$stock->stock_name = $request->get('stock_name');
$stock->stock_qty = $request->get('stock_qty');
$stock->stock_unit = $request->get('stock_unit');
$stock->stock_price_per_kg = $request->get('stock_price_per_kg');
$stock->stock_weight_per_qty = $request->get('stock_weight_per_qty');
$stock->save();
return redirect('/Stock/index/')->with('success', 'Stock updated!');
}
These are my routes这些是我的路线
//Route for Stock
Route::get('/Stock/createStock/', 'ChicController@createStock')->name('createStock');
Route::post('/Stock/createStock/', 'ChicController@storeStock')->name('storeStock');
Route::get('/Stock/index/', 'ChicController@indexStock')->name('indexStock');
Route::get('/Stock/edit/{id}', 'ChicController@editStock')->name('editStock');
Route::post('/Stock/edit/{id}', 'ChicController@updateStock')->name('updateStock');
Route::delete('/Stock/index/{id}', 'ChicController@destroyStock')->name('deleteStock');
This is my edit.blade.php这是我的 edit.blade.php
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Update a Stock</h1>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
<br />
@endif
<form method="post" action="{{ route('updateStock', $Stock->id) }}">
{{ csrf_field() }}
<div class="form-group">
<label for="stock_name">Stock Name:</label>
<input type="text" class="form-control" name="stock_name" value={{$Stock->stock_name }} />
</div>
<div class="form-group">
<label for="stock_qty">Stock Amount:</label>
<input type="number" class="form-control" name="stock_qty" value={{$Stock->stock_qty }} />
</div>
<div class="form-group">
<label for="stock_unit">Stock Unit:</label>
<select id="stock_unit" name="stock_unit" value={{$Stock->stock_unit}}>
<option value="Kg">Kg</option>
<option value="Qty">Qty</option>
</select>
</div>
<div class="form-group">
<label for="stock_price_per_kg">Price Per Kg:</label>
<input type="number" class="form-control" name="stock_price_per_kg" value={{$Stock->stock_price_per_kg }} />
</div>
<div class="form-group">
<label for="stock_weight_per_qty">Weight Per Qty:</label>
<input type="number" class="form-control" name="stock_weight_per_qty" value={{$Stock->stock_weight_per_qty }} />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
@endsection
I have tried everything, but I could not solve the problem.我已经尝试了一切,但我无法解决问题。 When I try to echo the $id in the edit Controller, it shows the correct $id.当我尝试在编辑 Controller 中回显 $id 时,它会显示正确的 $id。 Hence, I do not know how to solve this, nor do I know why this is happening.因此,我不知道如何解决这个问题,也不知道为什么会这样。
For your editStock
method you are already receiving an instance of a Stock
model matching the id
from the route parameter due to the Implicit Route Model Binding that is taking place.对于您的editStock
方法,由于正在进行的隐式路由 Model 绑定,您已经收到与路由参数中的id
匹配的Stock
model 实例。 When you pass that $id
variable to Stock::find($id)
you are passing a Model which is Arrayable
.当您将该$id
变量传递给Stock::find($id)
时,您将传递一个 Model ,它是Arrayable
。 This causes find
to become findMany
thinking you are passing many ids.这会导致find
变为findMany
认为您传递了许多 id。 You end up with a Collection because of this:因为这个,你最终得到了一个 Collection:
public function editStock(Stock $id)
{
$Stock = Stock::find($id);
return view('Stock.edit', compact('Stock', 'id'));
}
$Stock
is a Collection because of passing many to find
. $Stock
是一个 Collection 因为传递了很多来find
。 I would adjust this to only pass the $id
variable to your view which is the Stock
that you want.我会将其调整为仅将$id
变量传递给您想要的Stock
视图。
public function editStock(Stock $id)
{
return view('Stock.edit', [
'Stock' => $id,
]);
}
Your view will now have a Stock
variable which is the Stock
that you wanted.您的视图现在将有一个Stock
变量,它是您想要的Stock
。
Laravel 5.8 Docs - Routing - Route Model Bindings - Implicit Bindings Laravel 5.8 文档 - 路由 - 路由 Model 绑定 - 隐式绑定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.