在 laravel php 中的另一个关系中使用它时关系未出现

Relationship not appearing in while using it in another relationship in laravel php

我试图在 Laravel Voyager 编辑视图中存储选中的复选框数据,但不幸的是 eloquent 关系在另一个关系中使用时没有显示。为了弄清楚我的问题,我将展示我的表格及其关系

这是游览模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Tour extends Model
{
    use HasFactory;

    protected $table = "tours";

    protected $fillable = [
        'address',
        'title',
        'description',
        'primary_image',
        'price',
        'image',
        'index_image',
        'attribute_id'
    ];

    public function tourHasAttr()
    {
        return $this->hasMany(TourHasAttribute::class, 'tour_id', 'id');
    }


}

this is the Tour_has_attributes model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class TourHasAttribute extends Model
{
    protected $table = "tour_has_attributes";

    protected $fillable = [
        'id',
        'tour_id',
        'attribute_id'

    ];
    public function attributeTitle()
    {
        return $this->hasOne(TourAttributes::class, 'id', 'attribute_id');
    }
}
//This is the Bread controller of voyager which I overrided
   $Tour= Tour::find($id);
        $TourCheckedAttributes = $Tour->tourHasAttr()->get();
        dd($TourCheckedAttributes);
        $allTourAttributes = TourAttributes::all();
        return Voyager::view($view, compact('dataType', 'dataTypeContent', 'isModelTranslatable', 'allTourAttributes', 'TourCheckedAttributes'));
   
}

使用 dd($TourCheckedAttributes) 之后;它清楚地表明 Tour_has_attributes 与 tour_attributes 的关系没有出现

我正在学习本教程,如有任何帮助,我们将不胜感激 https://www.youtube.com/watch?v=BntEU1Q5ga8&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=20

可能你误解了模型之间的关系。

$TourCheckedAttributes = $Tour->tourHasAttr()->get();

这将 return $Tour 下的所有 tour_has_attr 行。

加载所有属性,可以这样写

$Tour->with('tourHasAttr')->get();

dd($Tour);

然后您将获得此游览下的所有 tour_has_attr 行。