Northify
Northify

Reputation: 391

Laravel - Query "with"

I have a query where I need to get info from 2 tables. So I need to get invoices with the invoice_status 1, where the user_status is 1. I though I could query the Invoices table with the users table. When I dd($invoices) I do get the invoice with the users just fine...

$invoices = Invoice::with('user')->where('invoice_status', '=', 2)->get();

But I'm not sure sure how I can get the users->account_statu. I tried this but get a query error it can not find account_status.

$invoices = Invoice::with('user')->where('invoice_status', '=', 2)->where('account_status', '=', 1)->get();

Upvotes: 6

Views: 50970

Answers (1)

rdiz
rdiz

Reputation: 6176

You can pass in a closure to handle the query for the eager-loaded content:

$invoices = Invoice::with(array('user' => function($query) {
    $query->where('account_status', '=', 1);
}))->where('invoice_status', '=', 2)->get();

Upvotes: 17

Related Questions