Search code examples
phplaravelinfyom

How to get value from different table in blade laravel?


I use laraver generator (infyom). In the table 'companies' there is partner_id but not the partner's name. I want display partner name that is in an other table named partners. How can I do this?

Companies (table.blade.php)

<div class="table-responsive">
    <table class="table" id="companies-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Partner ID</th>
                <th>Type</th>
                <th>Nip</th>
                <th>Street</th>
                <th>Building Num</th>
                <th>Apartment Num</th>
                <th>Postal Code</th>
                <th>Place</th>
                <th>Floor</th>
                <th colspan="3">Action</th>
            </tr>
        </thead>
        <tbody>
        @foreach($companies as $company)
            <tr>
                <td>{{ $company->name }}</td>
                <td>{{ $company->partner_id }}</td>
                <td>{{ $company->type }}</td>
                <td>{{ $company->NIP }}</td>
                <td>{{ $company->street }}</td>
                <td>{{ $company->building_num }}</td>
                <td>{{ $company->apartment_num }}</td>
                <td>{{ $company->postal_code }}</td>
                <td>{{ $company->place }}</td>
                <td>{{ $company->floor }}</td>
                <td>
                    {!! Form::open(['route' => ['companies.destroy', $company->id], 'method' => 'delete']) !!}
                    <div class='btn-group'>
                        <a href="{{ route('companies.show', [$company->id]) }}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a>
                        <a href="{{ route('companies.edit', [$company->id]) }}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                        {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
                    </div>
                    {!! Form::close() !!}
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>


Solution

  • You can use the relationship feature that Eloquent provides to relate the Company model to the Partner model via this partner_id field on Company:

    class Company ...
    {
        public function partner()
        {
            return $this->belongsTo(Partner::class);
        }
    }
    

    In your Controller you can eager load the relationship for all the Company models passed to the view

    public function index()
    {
        return view('companies.index', [
            'companies' => Company::with('partner')->paginate(...),
        ]);
    }
    

    In the view you can access the Partner for each Company:

    @foreach ($companies as $company)
        ...
        {{ $company->partner->name ?? 'none'}}
        ...
    @endforeach
    

    Laravel 7.x Docs - Eloquent - Relationships - One To Many (Inverse) belongsTo

    Laravel 7.x Docs - Eloquent - Relationships - Eager Loading with

    Laravel 7.x Docs - Eloquent - Relationships - Dynamic Properties