0

So this is the query I have constructed so far

$data['requested_units'] = RequestedUnit::with([
    'user',
    'agent_warrants' => function ($q) {
        $q->where('type', RequestedUnit::AgentSecurityWarrant);
    }
])
->where('status', RequestedGrant::REQUESTED)
->get();

the problem is that it assumes the 'type' column is in the table 'agent_warrants' but actually it is in the parent table i-e RequestedUnit , how to specify this?

7
  • ->with([ 'agent_warrants' => function($query) { $query->where('type',RequestedUnit::AgentSecurityWarrant) }]) Commented May 27, 2024 at 11:38
  • this assumes that type column is in agent_warrants , i want to specify that type columns is in the parent table requested_unit Commented May 27, 2024 at 11:40
  • You want to load agent_warrants when the type of RequestedUnit is RequestedUnit::AgentSecurityWarrant ? Commented May 27, 2024 at 12:01
  • @Maraboc yes , exactly Commented May 27, 2024 at 12:13
  • @Arsalan Khan can't you do just RequestedUnit::where('type',RequestedUnit::AgentSecurityWarrant) }]) and then ->with(['agent_warrants']) Commented May 27, 2024 at 12:47

1 Answer 1

0

You can't specify that in the query. with will load the relationships for every result.

However, you can use load after getting the results on a subset you obtain with a filtering method such a where, filter or reject

$data['requested_units'] = RequestedUnit::with([
    'user',
])
->where('status', RequestedGrant::REQUESTED)
->get();

$data['requested_units']
    ->where('type', RequestedUnit::AgentSecurityWarrant) // collection method
    ->load('agent_warrants');
Sign up to request clarification or add additional context in comments.

Comments

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.