我想检索与每个模型相关的标签,而不加载中间表的所有列。我想在索引页面上显示标签。请告诉我这种情况是否存在解决方案。
我尝试使用hasManyThrough关系来获取标签,但由于多态索引表而无法正常工作。
(表名称=>表列)
food_index => id,描述,food_type,food_id
水果=> ID,说明,价格,库存情况
蔬菜=> ID,描述,价格,可用性,卡路里
谷物=> ID,描述,价格,可用性,品牌
标签=> id,标签
(数据透视表) taggable => id,tag_id,taggable_type,taggable_id
模型=>
class Fruit extends Model
{
public function index()
{
return $this->morphOne('App\FoodIndex', 'indexable', 'food_type', 'food_id');
}
public function tags() {
return $this->morphToMany('App\Tag', 'taggable');
}
}
class Vegetable extends Model
{
public function index()
{
return $this->morphOne('App\FoodIndex', 'indexable', 'food_type', 'food_id');
}
public function tags() {
return $this->morphToMany('App\Tag', 'taggable');
}
}
class Cereal extends Model
{
public function index()
{
return $this->morphOne('App\FoodIndex', 'indexable', 'food_type', 'food_id');
}
public function tags() {
return $this->morphToMany('App\Tag', 'taggable');
}
}
class Tag extends Model
{
public function fruits() {
return $this->morphedByMany('App\Fruit', 'taggable');
}
public function vegetables() {
return $this->morphedByMany('App\Vegetable', 'taggable');
}
public function cereals() {
return $this->morphedByMany('App\Cereal', 'taggable');
}
}
class FoodIndex extends Model
{
public function owner()
{
return $this->morphTo("owner", "food_type", "food_id");
}
public function tags()
{
// return $this->hasManyThrough("App\Tag", "App\FoodIndex", "food_id", 'food_type', "taggable_id", "taggable_type");
// return $this->hasManyThrough('App\Tag', $this->food_type, 'id', 'id');
}
}
控制器中的代码=>
$index = FoodIndex::whereRaw("MATCH description AGAINST (? IN BOOLEAN MODE)", [$this->fullTextWildcards($term)])->with([
'tags'
])->simplePaginate(5);
我希望$ index-> tag属性返回与每个食品相关的标签 但我遇到错误