I would like to fetch all models that don't have a relationship.
Similar to fetching models which have one:
return $this->model->has('quote')->orderBy('created_at', 'desc')->get();
I would basically like to have the inverse of this. I can't find any documentation to do this though.
Basically this query:
SELECT * FROM custom_design_requests
RIGHT JOIN quotes
ON quotes.`custom_design_request_id` = `custom_design_requests`.id
But I'd like to avoid having to use the Query Builder (DB::table('custom_design_requests')->rightJoin('quotes', 'quotes.custom_design_request_id', '=', 'custom_design_requests.id')->get();
), so that I have a collection of instances of the model.
Thanks
I believe this is better than the accepted solution:
$this->model->doesntHave('quote')->orderBy('created_at', 'desc')->get();
This is described in more detail here:
https://laravel.com/docs/5.4/eloquent-relationships#querying-relationship-absence
You may try something like this:
$this->model->has('quote', '<', 1)->orderBy('created_at', 'desc')->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With