DavidBoe
DavidBoe

Reputation: 17

Laravel Eloquent - Multiple NOT LIKE queries?

I have a changeable output from the database like this:

1,2,3,4 and sometimes 5,4,9

I want to add a not like where clause depending on the user:

For example:

User 1

$products = \App\Product::where('user', '1')
    ->where(['category', 'not like', '1,2,3,4')
    ->get();

User 2

$products = \App\Product::where('user', '2')
    ->where(['category', 'not like', '5,4,9')
    ->get();

Upvotes: 0

Views: 1668

Answers (5)

Elie Morin
Elie Morin

Reputation: 1514

This is an answer that extend the Roman Bobrik's an Eli's.

The goal is to let your request to be able to handle a default condition while handling specific request when you need to.

if ($request->input('id') == 1) {$categories = [1,2,3,4];}
if ($request->input('id') == 2) {$categories = [5,4,9];}

$products = \App\Product::where('user', $request->input('id'))
    ->when(isset($categories), function($query) use($categories) { // This will be applied for specified users. In this case, users id = 1 || 2
        return $query->whereNotIn('category', $categories);
    })->when(!isset($categories), function($query) { // This will be applied when users id != 1 || 2
        return $query->whereNotIn('category', [1,2]); //Define your default categories for every users that are not specified at the begining.
    })->get();

You could also save the categories array to the users table and change the when() condition for when(Auth::user()->categories != null) and when(Auth::user()->categories == null).

The only warning is, when you use that kind of query, you must have two when() condition that are the exact opposite. Because if none of the both condition are respected, the final query will be $products = \App\Product::where('user', $request->input('id'))->get(); and I'm sure that you won't that to happen.

Well, you can chain more than two when() condition, but be sure to have one that will catch everything that was not catch by any other.

Upvotes: 0

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

Another way

 $user_id = $request->input('id');//Put your user id in variable either by request or anyway you want to save
 $category_arr = [];

 if($user_id == 1) {
   $category_arr = [1,2,3,4];   
 } elseif ($user_id == 2) {
   $category_arr = [5,4,9];
 } else {
   echo "Error Msg"
 }

$products = \App\Product::where('user',user_id)
   ->whereNotIn('category', $category_arr)
   ->get();

Upvotes: 0

Jonas Pardon
Jonas Pardon

Reputation: 7

If your changeable output is an array, you can try this:

$categories = [1, 2, 3, 4];

$products = \App\Product::where('user', 1)
   ->whereNotIn('category', $categories)
   ->get();

Upvotes: -1

Eli
Eli

Reputation: 984

If all you need is to change the category them why not make it a variable like this.

$categories = $userId == 1 ? [1,2,3,4] : [5,4,9];

$products = \App\Product::where('user',$userId)
  ->whereNotIn('category', $categories)
  ->get();

So my guess is that you need to have some method to see which categories a user should have that would be more complex than this, but well I hope you get the idea.

Upvotes: 0

Roman Meyer
Roman Meyer

Reputation: 2872

https://laravel.com/docs/5.8/queries#where-clauses

$products = \App\Product::where('user','2')
  ->whereNotIn('category', [5, 4, 9])
  ->get();

Upvotes: 2

Related Questions