Khalid AL-Damook
Khalid AL-Damook

Reputation: 55

query database using variable in eloquent

I am trying to query for records using eloquent.

this works fine:

    $xs = X::all()->where('y_id', 1);
    return view('x', compact('xs'));

but when i try to pass a variable:

    $xs = X::all()->where('y_id', $id);
    return view('x', compact('xs'));

it returns empty array!

how can I do this query?

Upvotes: 2

Views: 736

Answers (1)

Shyam Achuthan
Shyam Achuthan

Reputation: 950

The query should be

 $xs = X::where('y_id', $id)->get();

all() returns all the records without any condition if you use a condition do a get() after all conditions queries

Upvotes: 1

Related Questions