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>
but I have other foreach for the employees, I need put the department name. Regards.
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.
I recommend reading the docs on this. I started typing an explanation, but it just started getting too long. :/
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community