我有一个Laravel应用程序和一个模型Category。它与自己有一个叫做parent的关系
Category.php
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_uuid', 'uuid');
}我试着做这样的URL,
例:domain.com/mobile-phones/smartphones (“手机”-类别的母公司,“智能手机”-类别本身)
在路由文件中,应该是:
Route::get('/{parent}/{category}', ...)->name('category');是否有一种方法来编写代码,所以我只使用一个路由参数?例如:
Route::get('/{category:parent}/{category}', ...)->name('category');也是一种方法,使在刀片文件时,我生成的url只使用类别。
route('category', ['parent' => $category->parent, 'category' => $category]); // No
route('category', $category); // Yes我尝试将该路由绑定如下:
/{category:parent}/{category}但是,它给出了参数category只能使用一次的错误。
发布于 2022-11-01 15:38:52
我会像这样定义路线
Route::get('/categories/{parent}/{category}')->name('category');因为它容易读懂
在刀片上,在设备上运行一个循环,为路线准备锚标签。
@foreach ($smartPhone as $phones)
<a href="/categories/{{ $smartPhone->parent->name }}/{{ $smartPhone->categories }}">
Categories
</a>
@endforeach或者如果要显示类别名称
@foreach ($smartPhone as $phones)
<a href="/categories/{{ $smartPhone->parent->name }}/{{ $smartPhone->categories }}">
{{ $smartPhone->category }}
</a>
@endforeachhttps://stackoverflow.com/questions/74277663
复制相似问题