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



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2GQHoyo
via IFTTT

تعليقات

المشاركات الشائعة من هذه المدونة

I am unable to figure out how to create payment collection request, after a form has been submitted in laravel

laravel, mysql transaction not working after failed one time