Я устанавливаю для клиентов отношения «многие ко многим» с отделами.
Миграция:
Код: Выделить всё
Schema::create('customer_department', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('customer_id');
$table->unsignedBigInteger('department_id');
$table->timestamps();
$table->foreign('customer_id')
->references('id')
->on('customers');
$table->foreign('department_id')
->references('id')
->on('departments');
});
Код: Выделить всё
public function department(){
return $this->belongsToMany('App\Department');
}
Код: Выделить всё
public function customer(){
return $this->belongsToMany('App\Customer');
}
Код: Выделить всё
{{$customer->department}}
or
@foreach($customer->department as $depo)
{{$depo->name}}
@endforeach
or
{{$customer->pivot->department_id}}
...
Код: Выделить всё
public function show(Customer $customer)
{
return view('/customers/customer', ['customer' => $customer]);
}
Подробнее здесь: https://stackoverflow.com/questions/658 ... ivot-table