N
N
Nubbb2021-01-25 16:24:54
Laravel
Nubbb, 2021-01-25 16:24:54

How to arrange pivot in Laravel?

there is a Tariff model

class Tariff extends Model
{
    use HasFactory;

    protected $table = 'tariffs';

    public function permissions()
    {
        return $this->belongsToMany(TariffPermission::class,'permission_tariffs','tariff_permission_id','tariff_id');
    }
}


Tariff privilege model TariffPermission (name, display_name, id)

class TariffPermission extends Model
{
    use HasFactory;

    protected $table = 'tariff_permissions';
}


Model for linking tariffs and privileges PermissionTariff (tariff_permission_id, tariff_id)

class PermissionTariff extends Model
{
    use HasFactory;

    protected $table = 'permission_tariffs';
}


output of all tariffs and privileges to them

$tariffs = Tariff::with('permissions')->get();

for some reason only one privilege is displayed, and not all to the tariff

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nubbb, 2021-01-25
@Nubbb

I don’t know how well I organized everything, I edited it here, everything worked

public function permissions()
    {
        return $this->belongsToMany(TariffPermission::class,'permission_tariffs','tariff_id','tariff_permission_id');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question