Laravel, how to determine if a query builder contains an "order"?

I am trying to go by default if the relationship does not yet have order. But if so, I am not using the default value.

$q = $this->items(); if (empty($q->orders)) { $q = $q->order(); } 

Note that order() is only the default method in a BaseModel class. For some reason, when I try to call the orders property in the query builder, it says Undefined property , although this is a public property in the Builder class.

I don’t know why, or how I can check it.

EDIT:

I set the complete example in the route:

 class Test extends \Illuminate\Database\Eloquent\Model {} $router->get('/test', function () { $test = new Test; $q = $test->select('id')->orderBy('id', 'desc'); $bindings = $q->getRawBindings(); var_dump($bindings); return 'test'; }); 

Is the dump just splashing out the query builder object, not the bindings?

+4
source share
2 answers

After some turning around, finally, everything worked out.

So this is actually an instance of Eloquent\Builder not Query\Builder , so you need to do:

 $q->getQuery()->orders 

This gives you access to the request object.

+5
source

It is not very safe to use properties directly. You can use getRawBindings for this .

Using the following code, you get all the bindings in a flattened array:

 $q->getRawBindings(); 

Now you just need to filter orders .

+1
source

All Articles