Laravel-datatables: Returned data - hasOne relation is being flattened into original array.

Created on 7 Jun 2017  Â·  1Comment  Â·  Source: yajra/laravel-datatables

My 'raw' laravel query returns data as expected. There is a hasOne relation called membership Eg

Query:

'
User::whereHas('membership', function ($query) use ($filters) {
$query->where('created_at', '>=', $filters['start_date']. ' 00:00:00')
->where('created_at', '<=', $filters['end_date']. ' 00:00:00');
})->subscribed()
->get()
'

Results:
"id" => 15 "name" => "XXXX" "email" => "XXXX" "created_at" => "2017-05-11 13:27:58" "updated_at" => "2017-05-11 13:28:14" "stripe_id" => "XXXX" "card_brand" => "Visa" "card_last_four" => "XXXX" "trial_ends_at" => null "status" => 1 "is_unsubscribed" => null "locale" => "en_GB" "status_display" => "Active" "membership" => array:12 [â–¼ "id" => 22 "user_id" => 15 "name" => "cult" "stripe_id" => "XXXXX" "stripe_plan" => "XXXX" "quantity" => 1 "trial_ends_at" => null "ends_at" => null "created_at" => "2017-05-11 13:28:15" "updated_at" => "2017-05-11 13:28:15" "membership_plan" => array:13 [â–¶] "membership_type" => array:17 [â–¶] ]

Using Datatables::of( SAME query as above ), flattens the membership result, and includes it in the user data - this overwrites attributes with the same name (eg 'name' )

Query:
$data = Datatables::of( User::whereHas('membership', function ($query) use ($filters) { $query->where('created_at', '>=', $filters['start_date']. ' 00:00:00') ->where('created_at', '<=', $filters['end_date']. ' 00:00:00'); })->subscribed() )->make(true);
Results:
id: "15", name: "cult", email: "XXXXX", created_at: "2017-05-11 13:28:15", updated_at: "2017-05-11 13:28:15", stripe_id: "XXXXX", card_brand: "Visa", card_last_four: "XXXX", trial_ends_at: "", status: "1", is_unsubscribed: "", locale: "en_GB", user_id: "15", stripe_plan: "cult_monthly", quantity: "1", ends_at: "", status_display: "Active", membership: ""

I can see in the querries of the returned result that there is a left join occuring, which must be causing the issues with col names being overwritten.
select * fromusersleft joinsubscriptionsonsubscriptions.user_id=users.id

I am using Laravel 5.4,
"yajra/laravel-datatables-oracle": "7.5"

Most helpful comment

Update- this documentation helped me https://datatables.yajrabox.com/eloquent/relationships

I just needed to add a select condition to the query
->select('users.*')

>All comments

Update- this documentation helped me https://datatables.yajrabox.com/eloquent/relationships

I just needed to add a select condition to the query
->select('users.*')

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  Â·  3Comments

nasirkhan picture nasirkhan  Â·  3Comments

jackrsantana picture jackrsantana  Â·  3Comments

t0n1zz picture t0n1zz  Â·  3Comments

hohuuhau picture hohuuhau  Â·  3Comments