Reputation: 85
How can I access a table column using a foreign key in blade so far I have this but it does not work.
Inquiry Model / relations
public function client()
{
return $this->belongsTo(Client::class);
}
public function device()
{
return $this->belongsTo(Device::class);
}
Client Model
public function inquiry()
{
return $this->hasMany(Inquiry::class);
}
public function devices()
{
return $this->hasMany(Device::class);
}
Device Model
public function inquiry()
{
return $this->hasMany(Inquiry::class);
}
Controller
public function index()
{
$inquiries = Inquiry::all();
return view('stream.index', compact('inquiries'));
}
View
@foreach($inquiries as $inquiryKey => $inquiryValue)
<tr>
// stream id is the foreign that table has a column with names ...
<th scope="row">{{ $inquiryValue->stream_id }}</th> // works
<th scope="row">{{ $inquiryValue->stream_id->name }}</th> // does not work -> trying to get property of none object
Upvotes: 0
Views: 5587
Reputation: 105
if you name it stream_id than i think it's better u have a table name streams, if u want to get the client data from inquiry, than u should have client_id column in your inquiry table, same for the device, add device_id column to your inquiry table
How to access it:
@foreach($inquiries as $inquiryKey => $inquiryValue)
{{$inquiryValue->client->name}}
{{$inquiryValue->device->name}}
@endforeach
Upvotes: 0
Reputation: 21681
You should try with this example like :
Updated answer
Controller
public function index()
{
$inquiries = Inquiry::all();
return view('stream.index', compact('inquiries'));
}
View file
@foreach($inquiries as $inquiryKey => $inquiryValue)
<tr>
// stream id is the foreign that table has a column with names ...
<th scope="row">{{ $inquiryValue->client->name }}</th> // works
</tr
@endforeach
Upvotes: 0
Reputation: 3297
You cannot access attribute name
of stream_id
, it's just an integer, what you may try is $inquiryValue->client->name
or i think, $inquiryValue->client->first()->name
Be sure to let Laravel know that stream_id
is the key used for your relations.
Upvotes: 2