我有一个PHP Laravel项目,我想添加2个变量的URL



我有一个Laravel PHP网站商店,在每个产品上,我想有它的代码和图像的URL。

我的路线
Route::post('product/{slug}/{image}', [ProductController::class, 'details'])->name('products.details');

我在控制器中的功能

public function details($id){
$product = DB::select('SELECT * from products where id=?', [$id]);
$category_id = DB::table('products')->where('id', $id)->value('categories_id');
$slug = DB::table('categories')->where('id', $category_id)->value('slug');
$image = DB::table('products')->where('id', $id)->value('image');
return view('products-details', ['product'=>$product], ['slug'=>$slug], ['image'=>$image]);
}

视图

<form action="{{ action('AppHttpControllersAdminProductController@details', $products->id) }}" method="post" enctype="multipart/form-data" >
@csrf
<div class="card">
<img src="{{ asset('images/' . $products->image) }}" style="height:200px"  />
<div class="container">
<h4><b>{{ $products->name }}</b></h4> 
<p>@if ($products->original_price != $products->selling_price)
<div class="pricegreen">
<h4>{{ $products->selling_price}}</h4>
</div>
<div class="pricered"> <h4> {{ $products->original_price }} </h4></div>
@else
<h4>{{ $products->selling_price }}</h4>
@endif</p> 
<button type="submit">Megtekintés</button>
</div>
</div>
</form>

我不需要使用数据,我只想在URL中显示它。

我已经尝试了几个变体,包括[]-s,它只传递第一个数据:

<form action="{{ action('AppHttpControllersAdminProductController@details', $products->id, $products->slug, $products->image) }}" method="post" enctype="multipart/form-data" >

缺少[Route: products.details] [URI: product/{slug}/{image}][缺少参数:image].

既然你已经给了你的路由器一个名字,你可以在刀片文件中使用route刀片函数。

<form action="{{ route('products.details', ['slug' => $product->slug, 'image' => $product->image) }}" method="post" enctype="multipart/form-data">

也可以在details函数中使用compact,如下所示

public function details($id){
$product = DB::select('SELECT * from products where id=?', [$id]);
$category_id = DB::table('products')->where('id', $id)->value('categories_id');
$slug = DB::table('categories')->where('id', $category_id)->value('slug');
$image = DB::table('products')->where('id', $id)->value('image');
return view('products-details', compact('product','slug','image');
}

最新更新