lundi 23 novembre 2015

Nested where with wherePivot fails

I have a Laravel Eloquent function;

Category::select('id')->where('parent_category_id', null)
            ->with('children')
            ->with(['children.consultants' => function ($query) use($consultantId) {
                $query->where('users.id', $consultantId)
                    ->with(['categories' => function($query) {
                        $query->wherePivot('status', CategoryRequestStatus::Accepted)->orWherePivot('status', CategoryRequestStatus::Requested);
                    }]);
            }])->get();

And it's executed query;

select `categories`.*, `categories_consultants`.`consultant_id` as `pivot_consultant_id`, `categories_consultants`.`category_id` as `pivot_category_id`, `categories_consultants`.`status` as `pivot_status`, `categories_consultants`.`created_at` as `pivot_created_at`, `categories_consultants`.`updated_at` as `pivot_updated_at` from `categories` inner join `categories_consultants` on `categories`.`id` = `categories_consultants`.`category_id` where `categories_consultants`.`consultant_id` in (4) and `categories_consultants`.`status` = 1 or `categories_consultants`.`status` = 0 

I want to make this query's last or to be in parentheses

select `categories`.*, `categories_consultants`.`consultant_id` as `pivot_consultant_id`, `categories_consultants`.`category_id` as `pivot_category_id`, `categories_consultants`.`status` as `pivot_status`, `categories_consultants`.`created_at` as `pivot_created_at`, `categories_consultants`.`updated_at` as `pivot_updated_at` from `categories` inner join `categories_consultants` on `categories`.`id` = `categories_consultants`.`category_id` where `categories_consultants`.`consultant_id` in (4) and (`categories_consultants`.`status` = 1 or `categories_consultants`.`status` = 0)

I've changed it like below to solve this but It gives an error BadMethodCallException in Builder.php line 2071: Call to undefined method Illuminate\Database\Query\Builder::orWherePivot()

Category::select('id')->where('parent_category_id', null)
            ->with('children')
            ->with(['children.consultants' => function ($query) use($consultantId) {
                $query->where('users.id', $consultantId)
                    ->with(['categories' => function($query) {
                        $query->where(function($query) {
                            $query->wherePivot('status', CategoryRequestStatus::Accepted)->orWherePivot('status', CategoryRequestStatus::Requested);
                        });
                    }]);
            }])->get();



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1N4n6dZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire