Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I deleted my previous post after re-reading the question.

Remember that this is a many-to-many relationship. So...

<td>{{$employee->departments}}</td>

That will not work because there are many departments. You need to loop through each department. You also need to specify what attribute you want to output. So you should change that code to something like this:

<td>
// Loop through each department
@foreach ($employee->departments as $department)
	// Output each department followed by a line break. You can take out the line break or change it to whatever you want.
	{{ $department->name_department }}<br>
@endforeach
</td>
Last updated 8 years ago.
0

but I have other foreach for the employees, I need put the department name. Regards.

0

I know. You should reread my answer. You created a many-to-many relationship. You can't just echo out a relationship, and expect PHP to magically know what you want. You need to loop through the collection of departments and get the names individually.

Last updated 8 years ago.
0

Ok you talk me about a Custom Collection?

0

I recommend reading the docs on this. I started typing an explanation, but it just started getting too long. :/

http://laravel.com/docs/5.1/eloquent-relationships

http://laravel.com/docs/5.1/collections

Last updated 8 years ago.
0

Mmm ok thank's bro! I go to read!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

njuarezgt njuarezgt Joined 2 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.