samedi 1 février 2020

How to access data of a pivot table ( many to many relationship laravel )

I'm trying to get data from pivot table and passing them with jquery ajax. I want to get the name of the book and the quantity of its.

My Model

class Order extends Model
{
    public function books(){
        return $this->belongsToMany(Book::class)->withPivot('quantity');
    }

}


class Book extends Model
{

    public function order(){
        return $this->belongsToMany(Order::class)->withPivot('quantity');
    }

}

This is my controller

public function detail($id){
        if($id){
            $order = Order::find($id);
            dd($order->books);
        }
    }

I dont know why it returned

Illuminate\Database\Eloquent\Collection {#350 ▼
  #items: []
}



Aucun commentaire:

Enregistrer un commentaire