3

I have a number of models and I have the relationships set up correctly I believe.

I want to retrieve all of the models from a table if and only if the active column of a foreign key table is true.

I have tried a number of variations but to no avail.

Below is the set up.

The table is question is called device, which points to a table called dep.

The DEVICE model has the following relationship to DEP

public function dep() {

    return $this->belongsTo('App\Dep');

}

The DEP model has a column called active, which is set to true or false.

What eloquent command should I use to return all Devices where the active field of the DEP table is true?

At the moment I am having to get all the DEP models and then only get the devices if the active field is set to true but I am sure there must be a more elegant approach.

Thanks.

2 Answers 2

4

If you want to limit the results based on the existence of a field in a related model, you want to take a look at the whereHas method.

$devices = Device::whereHas('dep', function($query) {
    $query->where('active', '=', true); // Replace true with 1 if you aren't using casts
})->get();

This will get all devices that have a dep with the active field of true. This will not fetch dep along with the Device though. If you want that, then just use whereHas with eager loading as you normally would do like this:

$devices = Device::with('dep')->whereHas('dep', function($query) {
    $query->where('active', '=', true);
})->get();
Sign up to request clarification or add additional context in comments.

Comments

4

You can simply add a filter to your relationship:

public function dep() {
    return $this->belongsTo('App\Dep')->where('active', true);
}

Or, if you prefer to keep the dep relation unaltered, you can consider to add another relation to retrieve only the active models:

public function depActive() {
    return $this->belongsTo('App\Dep')->where('active', true);
}

This will return all the DEP models related to a Device, having active = 1.

You can use the relation as follow:

$depActives = $device->depActive;

or

$depActives = $device->depActive()->get();

boh of them will do the same

8 Comments

Very interesting, thank you. But what would the Eloquent command be to get only those Devices where the DEP active field is true? I tried, DEP::depActive()... but got the following error: Non-static method App\Device::depActive() should not be called statically, assuming $this from incompatible context
The actual line of code was: $data[] = Device::depActive()->get(); which caused the "Non-static method App\Device::depActive() should not be called statically, assuming $this from incompatible context" error.
I actually want all of the DEVICE models who's DEP is active. I don't need the DEP model at all in this case. I hope that makes sense?
Doesn't "Devices::with('dep')->where('active', true)->get();" assume that the DEVICES table has an active field? If I am reading it right it would return th DEP model with the DEVICE and only return Devices that have a field called "active" set to true. My Devices table has no such field.
No, it will join the Device table with the dep table (using your dep relation ) then it will filter the joined table by the active field
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.