缺少所需参数[Route: admin.][URI: admin/product/edit/{product_slug}



错误如下:缺少[Route: admin]的必需参数。[URI: admin/product/edit/{product_slug}][缺少参数:product_slug]。(观点:C: xampp 根 laravel 资源 views livewire admin admin-product-component.blade.php)

这是我的web.php:

Route::get('/admin/product/edit/{product_slug}',AdminEditProductComponent::class)->name('admin.editproduct');

在我的admin-product-component.blade.php中我添加了这个:

<div>
<div class="admin"  style="padding: 30px 0;">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class="col-md-6">
<a href="{{route('admin.addproduct')}}" class="btn btn-success pull-right">Add New</a>
</div>
</div>
</div>
<div class="panel-body">
<table class="table">
<thead class="thead-dark">
<tr>
<th class="table">Id</th>
<th class="table">image</th>
<th class="table">Name</th>
<th class="table">Price</th>
<th class="table ">Category</th>
<th class="table ">Date</th>
<th class="table ">Action</th>
</tr>
</thead>
</tbody>
@foreach ($products as $product)
<tr>
<td>{{$product->id}}</td>
<td><img src="{{asset('storage/imagg')}}/{{$product->image}}" width="60"></td>
<td>{{$product->name}}</td>
<td>{{$product->regular_price}}</td>
<td>{{$product->category->name}}</td>
<td>{{$product->created_at}}</td>
<td>
<a href="{{route('admin.editproduct',['product_slug'=>$product->slug])}}"><i class="fa fa-edit fa-2x text-info"></i></a>
</td>
</tr>
@endforeach
</tbody>
</table>
{{$products->links()}}
</div>

最后AdminEditProductComponent:

use LivewireComponent;
use AppModelsCategory;
use AppModelsProduct;
use IlluminateSupportStr;
use CarbonCarbon;
use LivewireWithFileUploads;
class AdminEditProductComponent extends Component
{
use WithFileUploads;
public $name;
public $slug;
public $short_description;
public $description;
public $regular_price;
public $sale_price;
public $featured;
public $quantity;
public $stock_status;
public $SKU;
public $image;
public $category_id;
public $newimage;
public $product_id;
public function mount($product_slug)
{
$product = Product::where('slug',$product_slug)->first();
$this->$name = $product->name;
$this->$slug = $product->slug;
$this->$short_description = $product->short_description;
$this->$description =  $product->description ;
$this->$regular_price =  $product->regular_price;
$this->$sale_price =  $product->sale_price;
$this->$featured =  $product->featured;
$this->$quantity =  $product->quantity;
$this->$stock_status =  $product->stock_status;
$this->$SKU =  $product->SKU;
$this->$image =  $product->image;
$this->$category_id =  $product->category_id;
$this->$product_id =  $product->product_id;
}
public function generateSlug()
{
$this->slug = Str::slug($this->name, '-');
}
public function updateProduct()
{
$product = Product::find($this->product_id);
$product->name = $this->name;
$product->slug = $this->slug;
$product->short_description = $this->short_description;
$product->description = $this->description;
$product->regular_price = $this->regular_price;
$product->sale_price = $this->sale_price;
$product->featured = $this->featured;
$product->SKU = $this->SKU;
$product->stock_status = $this->stock_status;
$product->quantity = $this->quantity;
if($this->newimage)
{
$imageName = Carbon::now()->timestamp.'.'. $this->newimage->extension();
$this->newimage->storeAs('imagg',$imageName,'public');
$product->image = $imageName;
}
$product->category_id = $this->category_id;
$product->save();
session()->flash('message', 'Product has been updated successfully !');
}
public function render()
{
$category = Category::all();
return view('livewire.admin.admin-edit-product-component',['categories'=>$categories])->layout('layouts.admin-category');
}
}

您可以将?

设置为可选
Route::get('/admin/product/edit/{product_slug?}',AdminEditProductComponent::class)->name('admin.editproduct');

您现在已经设置了关键参数,您正在使用slug,但laravel正在使用id来查找模型,但您正在传递slug有两种方法在你的路由中像这样定义键

Route::get('/admin/product/edit/{product:slug}',AdminEditProductComponent::class)->name('admin.editproduct');

或者在你的模型中定义

public function getRouteKeyName()
{
return 'slug';
}

最新更新