Hello, i am creating a book store website but now i have three tables in relation book, orders, users i made the relation as below code note: i setted the order as peviot
class Book extends Model
{
public function users(): belongsToMany
{
return $this->belongsToMany(User::class)->using(Order::class);
}
}
class Order extends Pivot
{
use HasFactory;
protected $table = 'orders';
protected $fillable = [
'order_date',
'book_id',
'customer_id',
'order_status',
'quantity',
'price',
];
#[ArrayShape(['order_date' => 'date', 'order_status' => 'integer'])]
protected function casts(): array
{
return [
'order_date' => 'date',
'order_status' => OrderStatus::class,
];
}
}
class User extends Authenticatable
{
public function books() :BelongsToMany
{
return $this->belongsToMany(Book::class)->using(Order::class);
}
}
User::factory(1)
->hasAttached(Order::factory(2))
->hasAttached(Book::factory(2))
->create();