Laravel - Query "with"
Asked Answered
E

1

6

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();
Evieevil answered 17/10, 2014 at 15:34 Comment(0)
D
17

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();
Dorthydortmund answered 17/10, 2014 at 15:36 Comment(2)
This is not working for me, I'm getting a syntax error, unexpected '[', expecting ')'Evieevil
Probably because of the new array syntax. See my edited answerDorthydortmund

© 2022 - 2024 — McMap. All rights reserved.