Reputation: 13
I'm trying to delete some elements from object taken from DB with Eloquent.
I have an object assigned to variable $words
$words = Word::all();
When I'm displaying $words as JSON it looks like so:
[
{
"id": "1",
"word": "dog",
"phonetic": "dog",
"mean": "pies",
"assoc": "some example text",
"author_id": "3",
"user_id": "3"
},
{
"id": "2",
"word": "sun",
"phonetic": "sun",
"mean": "słońce",
"assoc": "lorem ipsun dolor sit amet",
"author_id": "3",
"user_id": "2"
}, ...
]
What I want to do is remove from whole object those entities which have, for example, value "user_id" set on 2, 4, or 5. This id's are stored in an array.
What's the best method to achieve it with Eloquent ORM? Using Word::where(...) isn't enough because, as far as I know, it can take only one value. I was trying to do it with foreach loop's but past iterating the object $words returned with no changes.
Upvotes: 1
Views: 651
Reputation: 9341
I didn't understand your question clearly, but you can use where statement as a closure and use your foreach loop inside if you want to bind more than one where statement.
Something like this may work;
$result = Word::where(function($query) use ($words) {
foreach($words $k => $v)
{
//Whatever you want to bind into where
$query->where($v, '=', 1);
}
})->delete();
Upvotes: 1
Reputation: 2847
How about using where_not_in()
?
http://laravel.com/docs/database/fluent
Like this:
DB::table('users')->where_not_in('id', array(2, 4, 5))->get();
Upvotes: 0