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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Hi Jeff, this does work as those properties are not undefined anymore. However, I set up a table like <th>PM</th><th>AM</th></thead><tbody><td>@if($employee->department == 'Project Manager'){{ $employee->name }}</td><td>@else($employee->department == 'Account Manager'){{ $employee->department }}</td></tbody> and this does not work to show the correct employees in their respective sections. Any ideas how to fix this?
@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.
added the update. Let me know if it looks ok or want me to edit further.
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.
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?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.