Php 使用Laravel 5.8中的Laravel嵌套集查询祖先
我正在使用并且很难查询我的类别模型的Php 使用Laravel 5.8中的Laravel嵌套集查询祖先,php,laravel,laravel-5,Php,Laravel,Laravel 5,我正在使用并且很难查询我的类别模型的祖先 这就是我想做的: 有效 (这给了我一个所有类别的列表) (这是所有嘉年华游戏的列表) 不起作用 (这显示的是嘉年华游戏列表,而不是大型游戏类别) 我相信这只是我的理解力不足——感谢您的帮助 web.php ... Route::get('/', 'CategoryController@index')->name('categories'); Route::get('/{category}/{slug?}', 'CategoryController@
祖先
这就是我想做的:
有效
(这给了我一个所有类别的列表)
(这是所有嘉年华游戏的列表)
不起作用
(这显示的是嘉年华游戏列表,而不是大型游戏类别)
我相信这只是我的理解力不足——感谢您的帮助
web.php
...
Route::get('/', 'CategoryController@index')->name('categories');
Route::get('/{category}/{slug?}', 'CategoryController@show')->name('show');
...
...
class CategoryController extends Controller
{
public function index()
{
$categories = Category::get()->toTree();
return view('categories.index', compact('categories'));
}
public function show(Category $category, $slug = null)
{
if (!is_null($slug)) {
$categories = Category::ancestorsOf($category);
return view('categories.show_subcategory', compact('categories', 'category', 'slug'));
} else {
return view('categories.show', compact('category'));
}
}
}
...
@foreach($category['children']->chunk(3) as $chunk)
<div class="row">
@foreach($chunk as $category)
<div class="col-md-4" style="margin-bottom: 2rem">
<div class="feature-box center media-box fbox-bg">
<div class="fbox-media">
<a href="/categories/{{$category->slug}}">
<img class="image_fade"
src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
alt="Featured Box Image"
style="opacity: 1;"></a>
</div>
<div class="fbox-desc">
<h3>{{$category->name}}</h3>
<span><a href="/categories/{{$category->slug}}">Learn More</a></span>
</div>
</div>
</div>
Products
@if(count($category->products) > 0)
@foreach($category->products as $product)
{{$product->title}}
@endforeach
@endif
@endforeach
</div>
@endforeach
...
CategoryController.php
...
Route::get('/', 'CategoryController@index')->name('categories');
Route::get('/{category}/{slug?}', 'CategoryController@show')->name('show');
...
...
class CategoryController extends Controller
{
public function index()
{
$categories = Category::get()->toTree();
return view('categories.index', compact('categories'));
}
public function show(Category $category, $slug = null)
{
if (!is_null($slug)) {
$categories = Category::ancestorsOf($category);
return view('categories.show_subcategory', compact('categories', 'category', 'slug'));
} else {
return view('categories.show', compact('category'));
}
}
}
...
@foreach($category['children']->chunk(3) as $chunk)
<div class="row">
@foreach($chunk as $category)
<div class="col-md-4" style="margin-bottom: 2rem">
<div class="feature-box center media-box fbox-bg">
<div class="fbox-media">
<a href="/categories/{{$category->slug}}">
<img class="image_fade"
src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
alt="Featured Box Image"
style="opacity: 1;"></a>
</div>
<div class="fbox-desc">
<h3>{{$category->name}}</h3>
<span><a href="/categories/{{$category->slug}}">Learn More</a></span>
</div>
</div>
</div>
Products
@if(count($category->products) > 0)
@foreach($category->products as $product)
{{$product->title}}
@endforeach
@endif
@endforeach
</div>
@endforeach
...
显示\u category.blade.php
...
Route::get('/', 'CategoryController@index')->name('categories');
Route::get('/{category}/{slug?}', 'CategoryController@show')->name('show');
...
...
class CategoryController extends Controller
{
public function index()
{
$categories = Category::get()->toTree();
return view('categories.index', compact('categories'));
}
public function show(Category $category, $slug = null)
{
if (!is_null($slug)) {
$categories = Category::ancestorsOf($category);
return view('categories.show_subcategory', compact('categories', 'category', 'slug'));
} else {
return view('categories.show', compact('category'));
}
}
}
...
@foreach($category['children']->chunk(3) as $chunk)
<div class="row">
@foreach($chunk as $category)
<div class="col-md-4" style="margin-bottom: 2rem">
<div class="feature-box center media-box fbox-bg">
<div class="fbox-media">
<a href="/categories/{{$category->slug}}">
<img class="image_fade"
src="https://*****.s3-us-west-1.amazonaws.com/{{ $category->photo }}"
alt="Featured Box Image"
style="opacity: 1;"></a>
</div>
<div class="fbox-desc">
<h3>{{$category->name}}</h3>
<span><a href="/categories/{{$category->slug}}">Learn More</a></span>
</div>
</div>
</div>
Products
@if(count($category->products) > 0)
@foreach($category->products as $product)
{{$product->title}}
@endforeach
@endif
@endforeach
</div>
@endforeach
...
。。。
@foreach($category['children']->chunk(3)作为$chunk)
@foreach($chunk作为$category)
{{$category->name}
产品
@如果(计数($category->products)>0)
@foreach($category->products as$product)
{{$product->title}
@endforeach
@恩迪夫
@endforeach
@endforeach
...
调用show()
函数时,“嘉年华游戏”的名称如何转换为$category
模型?另外,你能展示一些样本数据吗?