lundi 3 février 2020

Retrieve data from pivot and their related tables laravel in single query

I have two tables with many to many relationship as follows

Buyer
  - id
  - Name
Book
 - id
 - BookName
Book_Buyer
  - id
  - book_id
  - buyer_id
  - Quantity
  - Price

I need to retrieve Name, BookName, Quantity and Price from this relation through a single query. I need to format the response into JSON where I can show them to yajra datatable

How can I do it? I tried using an array where I pushed them through a single array but I didn't get the result in to the datatable.

$get = Buyer::all()->each(function ($buyer) {
  $buyer->books->map(function ($books) {
    return  $books->pivot;
  });
});

I tried the above solution but it gives me the whole values from database as in relation with pivot. Can anyone suggest the way to do it



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire