Access relation of pivot table in Laravel

Giorgio

I have three models Bill, Product and Process. Bill has a ManyToMany relationship with Product and the pivot table have some extra fields. I write the Bill model class like follow:

<?php
    class Bill extends Model
    {
           function products(){
                return $this->belongsToMany(\App\Product::class)
                    ->withPivot('process_id') // the id of the Process 
                    ->withTimestamps();
            }
    }

The Process model is a simple table with an id and a name. I am associating the id in the pivot table for reference the Process, the name could change over time but still referring the same concept so I can't associate the name.

The show page for a single Bill lists the products associated in a table like follow:

@foreach($bill->products as $product)
     <tr>
        <td>{{$product->barcode}}</td>
        <td>{{$product->pivot->process_id}}</td> 
     </tr>
@endforeach

So the problem is that I need the name of the process, but I have the id. I'm not sure how I could get the name.

Thanks

Tschitsch

I think you can use an own Pivot Model, e.g. ProductBill in order to achieve this.

class ProductBill extends Pivot {

    public function process() {
        return $this->belongsTo(Process::class);
    }

}

By using this model in your products relation on Bill

class Bill extends Model {

    function products() {
        return $this->belongsToMany(\App\Product::class)
            ->withPivot('process_id')
            ->using(ProductBill::class)
            ->withTimestamps();
    }

}

When accessing $product->pivot you should get an instance of MyCustomPivotModel now, hence you should be able to do the following:

<td>{{$product->pivot->process->name}}</td> 

(Unfortunatelly I not able to doublecheck right now :/)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i filter pivot table with One to Many relation inside - Laravel 7

How to insert pivot table in case of many to many relation? (Laravel 5.3)

Laravel observer for pivot table

How can I write a relation for pivot table in Laravel?

L5.6 - Relation on pivot table

retrieve data from pivot table many to many relation ship laravel 5

Laravel - one-to-one relation through pivot table with eager load

Laravel relation from a pivot

Setting property in pivot table in many to many relation

One-to-one relation through pivot table

Laravel - Access additional column data within a pivot table

Laravel - Can't access pivot table data

How to assign relation when working with pivot table in Laravel 4?

Eloquent relation pivot table with table

ManyToMany relation - how update attribute in pivot table

Laravel eloquent with pivot table

Laravel relationship with pivot table

Laravel Eloquent get a relation that matches a value in pivot table

Laravel L5.5 No access to pivot table in "Many to many" relationship

PHP-Laravel get data from pivot table in manytomany relation

How to access Pivot in Laravel?

How to limit access from pivot table? Laravel

inserting data into database laravel 5.6 relation belongstomany pivot table

Laravel many many relation cannot get pivot table

In Laravel, how can I access data from a pivot table?

Laravel 5.8 many to many relation using custom column names for pivot table

laravel 5.8 access other table's property in one to many relation

pivot table is not attaching laravel relation

Access Index of Pivot Table in Laravel