user7456955
user7456955

Reputation:

laravel how to validate if and or in controller

Actually maybe this is question I asked before but I didn't get a good answer, I have a customer table and want to check if package_id equal 1 or 2 or 3 or 4 then do something else if its null do something else, so all I need just to check if its not null or greater than 0 this will work I tried this code but it will not care about if it is null

 $query = Customer::where('customer_id', 'LIKE', $request->customer_id)
              ->where('package_id','>', 0)
              ->get();

 if(!empty($query) && count($query) > 0){ 
     //do something
 } else { 
     //do something else
 }

Upvotes: 0

Views: 873

Answers (2)

Christian Maioli M.
Christian Maioli M.

Reputation: 587

You can add whereNotNull to your query for that.

$query = Customer::where('customer_id', 'LIKE', $request->customer_id)
             ->where('package_id','>', 0)
             ->whereNotNull('package_id')
             ->get();

if (!empty($query) && count($query) > 0) { 
    //do something
} else {
    do something else
}

Upvotes: 0

YouneL
YouneL

Reputation: 8351

You have to use whereNotNull method to remove null values from your query, Also You can count the number of rows directly in your query Like this :

$count = Customer::where('customer_id', $request->customer_id)
                ->where('package_id','>', 0)
                ->whereNotNull('package_id')
                ->count();

if( $count > 0 ) { 
    //do something
} else { 
    //do something else
}

Upvotes: 1

Related Questions