通过 Laravel 刀片视图中的多个关系获取外键名称

问题描述

我试图通过 Laravel 刀片视图中多个外键名称的关系来获取外键名称,但它没有带回外键名称并给我一个“试图获取属性 'location'非对象

在我的刀片视图中,我试图获取这样的外键名称

<input type="text" class="form-control" value="{{ $document->office->location->location_name }}" disabled> 

这是我的文档模型:

class Document extends Model
{

    protected $fillable = [
      'doc_code','doc_name','doc_description','office_id',];

    public function office() {
      return $this->belongsTo(Office::class);
    }
}
 

这是我的办公室模型:

class Office extends Model
{

    protected $fillable = [
      'location_id','office_name',];

    public function location() {
      return $this->belongsTo(Location::class);
    }
}
 

这是我的位置模型:

class Location extends Model
{

    protected $fillable = [
      'location_name','location_address',];

}
 

这是我在我的文档控制器中的视图的代码

public function show($id)
    {
        $office = Office::all();
        $document = Document::find($id);

        return view('documents.show',compact('document','office'));
    } 

为了根据上述另一个外键检索外键名称,我缺少什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)