Javascript 基于以前的ID获取记录-Laravel
请告诉我,我想根据其Javascript 基于以前的ID获取记录-Laravel,javascript,php,laravel,laravel-5.3,Javascript,Php,Laravel,Laravel 5.3,请告诉我,我想根据其类别id获取作物,如果可以在不使用JavaScript的情况下实现这一点,我将非常高兴 显示页面 <div class="cat-list"> <h3 class="cat-title"> <a href="/filterByCategory/{{$category->cat_id}}"> <i class="fa fa-car ln-shadow"></i>
类别id
获取作物,如果可以在不使用JavaScript的情况下实现这一点,我将非常高兴
显示页面
<div class="cat-list">
<h3 class="cat-title">
<a href="/filterByCategory/{{$category->cat_id}}">
<i class="fa fa-car ln-shadow"></i>
{{$category->cat_name}}<span class="count">{{$category->count()}}</span>
</a>
<span data-target=".cat-id-1" data-toggle="collapse" class="btn-cat-collapsed collapsed">
<span class=" icon-down-open-big"></span>
</span>
</h3>
<ul class="cat-collapse collapse in cat-id-1">
@php
$catID = $category->cat_id;
//want to fetch crops base on the $catID and display in foreach
@endphp
</ul>
</div>
作物模型
public function category()
{
return $this->belongsTo(Category::class, 'cat_id', 'crop_cat')
}
public function category()
{
return $this->hasmany(Category::class, 'crop_cat','cat_id')
}
类别模型
public function category()
{
return $this->belongsTo(Category::class, 'cat_id', 'crop_cat')
}
public function category()
{
return $this->hasmany(Category::class, 'crop_cat','cat_id')
}
您可以将此方法添加到类别模型中:
public function crops() {
return $this->hasMany(Crop::class, 'catID');
}
此功能适用于作物模型:
public function category() {
return $this->belongsTo(Category::class, 'catID');
}
之后,您基本上可以循环浏览刀片模板上的所有类别,并在此循环中回显当前类别的所有作物。然后,这可能看起来像这样:
@foreach ($categories as $category)
{{ $category->name }}
@foreach ($category->crops as $crop)
{{ $crop->title }}
@endforeach
@endforeach
您可以在Laravel上找到更多关于雄辩关系的信息。您还没有概述作物与类别的关系。发布数据库架构和/或模型关系。