1

I've looked at other questions that have answered this same error and I understand I have a property that cannot be defined but not sure how to make it defined. First time using Many-to-Many relationship so I'm assuming this is the issue.

Project Table id company stage status date_started date_finished timestamps

Employee Table id name department timestamps

Employee_Project Table id employee_id project_id

In both models, I have belongsToMany and for the show function in the ProjectController, I have:

$projects = Project::find($id);

In the view, I want to show company name ($projects->company) and the employees on that project with their respective departments. These do not work:

$projects->employees->name

$projects->employees->department

How do I access these properties?

--Update-- Jeff's answer works but I set up a table like <thead><th>PM</th><th>AM</th></thead>

<tbody><td>@if($employee->department == 'Project Manager'){{ $employee->name }}</td>

<td>@else($employee->department == 'Account Manager'){{ $employee->name }}</td></tbody>@endif

and this does not work to show the correct employees in their respective sections. Any ideas how to fix this?

robk27
  • 683
  • 1
  • 12
  • 23

2 Answers2

2

Your problem is that $project->employees is a collection, not an individual instance of an employee. You will need to iterate over the collection in order to access each individual employee's name:

foreach($project->employees as $employee) {
    echo $employee->name;
    echo $employee->department;
}

Update

It looks like you may need to restructure as below, I think you probably want the entire if construct to be within the same table cell, though I could be wrong:

<tbody>
    @foreach($project->employees as $employee)
    <tr>
        <td>
        @if($employee->department == 'Project Manager')
            {{ $employee->name }}
        @elseif($employee->department == 'Account Manager')
            {{ $employee->department }}
        @else
            <!-- What about non project/account managers? -->
        @endif
        </td>
    </tr>
    @endforeach
</tbody>

I could be totally wrong, and maybe you want account managers and project managers in different columns:

<tr>
    <td>
    @if($employee->department == 'Project Manager')
        {{ $employee->name }}
    @endif
    </td>
    <td>
    @if($employee->department == 'Account Manager')
        {{ $employee->name }}
    @endif
    </td>
</tr>

Be sure to take a look at the documentation on Blade if statements.

Update 2

If you want to order how the related entities are coming back, you can do something like this (assuming you want the employees ordered by their department:

$project->employees()->orderBy('department', 'DESC')->get();

Notice you're no longer accessing the property, you're calling the relation method employees() so that you can modify the query prior to it being executed.

Here's a link to the documentation on ordering queries, and there is also another StackOverflow question on it.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Hi Jeff, this does work as those properties are not undefined anymore. However, I set up a table like PMAM@if($employee->department == 'Project Manager'){{ $employee->name }}@else($employee->department == 'Account Manager'){{ $employee->department }} and this does not work to show the correct employees in their respective sections. Any ideas how to fix this? – robk27 Mar 08 '17 at 22:07
  • @robk27 Try adding that update to your question, its a bit difficult to make out if what I'm seeing is correct or just a copy/paste mistake. – Jeff Lambert Mar 08 '17 at 22:08
  • added the update. Let me know if it looks ok or want me to edit further. – robk27 Mar 08 '17 at 22:12
  • Sorry. Updated my code. I had the @endif in my code and I wanted $employee->name for both parts. It is taking it in the order of the employee_project table rather than using the if statement and checking for the $employee->department to put the respective names in. – robk27 Mar 08 '17 at 22:19
  • I'm not sure I understand. Is the issue that some employees are being left out, or they are being shown in the wrong order, or something else? – Jeff Lambert Mar 08 '17 at 22:21
  • The wrong order. As in the if statements are not being used at all and it's just taking it from the order that is in the relation table. – robk27 Mar 08 '17 at 22:24
  • So if I have employee 1 as a project manager and employee 2 as account manager. Employee 2 is showing up under the if statement where it says @if($employee->department == 'Project Manager') – robk27 Mar 08 '17 at 22:27
  • @robk27 Its still a bit ambiguous as to whether you need to modify the query (as in my update 2) or if there's an issue with your HTML structure or logic. I changed the snippet of my first update, so try to mirror that change if the order of the query isn't what you want. – Jeff Lambert Mar 08 '17 at 22:31
  • Thanks @JeffLambert. To explain what I'm trying to do let me write it out. There are multiple projects. Each project has a PM and an AM. When I click on a project to get more info, I want to see the the PM and AM for that project. So projects have multiple employees and employees have multiple projects. Does that make sense? Are my tables set up correctly? – robk27 Mar 08 '17 at 22:46
  • Just saw your updated answer. Let me try it with two different if statements. – robk27 Mar 08 '17 at 22:47
  • @robk27 Ohh.. ok. I had understood AM/PM to be morning/afternoon, sorry about that. – Jeff Lambert Mar 08 '17 at 23:28
  • Oh no problem. Thank you for helping me out. I am away from my computer right now but will try out the separate if statements to see if that fixes it. – robk27 Mar 08 '17 at 23:54
0

So I figured out how to show the correct person to show up under their respective departments in the table and I wanted to share.

Thanks to @jeff-lambert I added the foreach loop but I had added to the entire row like in his first update. This did not work because the employees were not showing up in their respective departments. For example, account managers were showing up under the project manager . To resolve this, I had to add a foreach loop to each .

<td>
  @foreach($projects->employees as $employee)
  @if($employee->department == 'Project Manager')
   {{ $employee->name }}
  @endif
  @endforeach
 </td>

Hope this helps explain it.

robk27
  • 683
  • 1
  • 12
  • 23