php - Getting foreign key name through multiple relationships in Laravel blade view
one text
I am trying to get a foreign key name through a relationship of multiple foreign key names in my Laravel blade view, but it isn't bringing back the foreign key name and giving me a "Trying to get property 'location' of non-object
In my blade view I am trying to get the foreign key name like this:
<input type="text" class="form-control" value="{{ $document->office->location->location_name }}" disabled>
This is my document model:
class Document extends Model
{
protected $fillable = [
'doc_code',
'doc_name',
'doc_description',
'office_id',
];
public function office() {
return $this->belongsTo(Office::class);
}
}
This is my office model:
class Office extends Model
{
protected $fillable = [
'location_id',
'office_name',
];
public function location() {
return $this->belongsTo(Location::class);
}
}
This is my location model:
class Location extends Model
{
protected $fillable = [
'location_name',
'location_address',
];
}
This is the code for my view in my Document Controller:
public function show($id)
{
$office = Office::all();
$document = Document::find($id);
return view('documents.show',compact('document', 'office'));
}
What am I missing in order to retrieve the foreign key name based on another foreign key like the above?
Source