Search code examples
phpmysqllaraveleloquentmany-to-many

Undefined property in Laravel 5


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?


Solution

  • 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.