dimanche 22 septembre 2019

Laravel Model Collection not Matching SQL

So Weird!

I am using the following code to create a query (Laravel 5.5). It is a little dynamic in nature. Here is the code:

$calls = Media::where('user_id', '=', $user->id)
        ->where(function($query){

            $query->where(function($qq){
                $qq->where('some_param', '=', 1)
                    ->where('some_other_param', '=', 'A');
            });

            $query->orWhere(function($qq){
                $qq->where('some_param', '=', 0)
                    ->where('some_other_param', '=', 'B');
            });

        })
        ->when(($completedOnly == true), function($q){
            $q->where('status', '>=', '8');
            $q->where(function($qq){
                $qq->where('s3_key', '!=', 0);
                $qq->where('s3_key', '!=', '');
                $qq->whereNotNull('s3_key');
                $qq->whereNotNull('s3_bucket');
            });
        })
        ->whereNotNull('some_third_param')
        ->whereNotNull('some_fourth_param')
        ->orderBy('sort_param', 'desc')
        ->offset($start)
        ->limit($amount)
        ->get();

    return $media;

I am using the Laravel Debugbar to help me out and make sure that the query is being created correctly. When I look at the readout, I see the following:

select * from `users_media` where `user_id` = '13475' and ((`some_param` = '1' and `some_other_param` = 'A') or (`some_param` = '0' and `some_other_param` = 'B')) and `status` >= '8' and (`s3_key` != '0' and `s3_key` != '' and `s3_key` is not null and `s3_bucket` is not null) and `some_third_param` is not null and `some_fourth_param` is not null and `users_media`.`deleted_at` is null order by `sort_param` desc limit 25 offset 0

When I plug that into my DB client to test the query, I see data given back as I would expect.

However, when I am trying to retrieve the data within Laravel and Eloquent, I am getting an empty collection. I don't understand why this is happening. If the query works with my client, why would I be getting an empty collection from Eloquent?

Help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire