Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NiloLeon's avatar

Substitute a code id with corrispondent other field in separate table of coding

Hi which is the best way to do this I have 3 tables

Invoice table

namespace App;

use Illuminate\Database\Eloquent\Model;

class Invoice extends Model
{


    /**
     * The roles that belong to the invoice.
     */
    public function shifts()
    {
        return $this->belongsToMany(Shift::class, 'invoice_shift')
        ->withPivot(['invoice_id','shift_id','shift_swapped_date','tas']);
    }
}

Shift table


use Illuminate\Database\Eloquent\Model;

class Shift extends Model
{

    /**
     * The users that belong to the role.
     */
    public function invoices()
    {
        return $this->belongsToMany(Invoice::class, 'invoice_shift');
    }
}

In relation many to many

and I have a third table User

with id and name field

In table pivot invoice_shift i have a column where i save user ID

how can get user->name in substitution with user id in blade??

Have I to create another relationship many to many between user table and invoice_shit pivot table? O there are another method?

Thanks a lot

0 likes
2 replies
NiloLeon's avatar

Maybe i solved adding in controller

$users = User::all();

then in blade with and @if and @foreach could be right?

Please or to participate in this conversation.