I Have the following code (member is just a standard Eloquent model)
$members = new Member;
$members->where('user_id', '=', 5);
$_members = $members->get();
The last query run produces "SELECT * from members
", so it seems to be ignoring my where clause, what am I doing wrong here?
By the way I know I could do $members = new Member::where(...)
etc... but I will be adding the where
clauses in a loop in order to create filtering on the results from the database.
UPDATE
The only way around this seems to be to add a where that will catch all on initialization such as:
$members = Member::where('member_id', '<>', 0);
$members->where('user_id', '=', 5);
$_members = $members->get();
But this seems quite a bit of a hack. I am not trying to do anything complicated so I cant be the only one who has had this problem?
FIXED MAYBE
For anyone who has stumbled here I have fixed this by using:
$members = Member::query();
$members->where('user_id', '=', 5);
$_members = $members->get();
Not sure if that is the correct way but it works for me and doesn't appear like a hack.
There is a much better way to achieve what you need here using query scopes. Here is what you need to do.
In your Member.php model do the following:
In your controller do this:
This is a very basic example that can be expanded upon depending on what you need. As you can see you can pass as many conditions as you require to the query scope.
I don't believe Eloquent works like that.
Try this...
Wouldn't you have to call find() instead of get() ?