Reputation: 208
The tables:
users
-id
-name
users_companies
-id
-user_id
-company_id
companies
-id
-name
users_companies is the pivot table
I want to show or get the "name" field of a table, this is my code in the table
<td>{{ $user->company }}</td>
but then it shows like this in the column of the table. I just want to get the value of the name like "VCY BT Holdings". Thanks
[{"id":5,"name":"VCY BT Holdings","deleted_at":null,"created_at":"2017-12-23 02:24:50","updated_at":"2017-12-23 02:24:50","pivot":{"user_id":14,"company_id":5}}]
enter image description here
Upvotes: 1
Views: 104
Reputation: 3538
This should solved, but make sure you have define many-to-many relationship in User.php
model
class User extends Model {
public function company()
{
return $this->belongsToMany('App\Company')
}
}
Then use loop to get each company
@foreach ($user->company as $company)
<li>{{ $company->name }}</li>
@endforeach
Upvotes: 2