where condition in laravel eloquent code example

Example 1: laravel where multiple conditions

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

Example 2: laravel where multiple conditions on single colmn

// here, i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();

Example 3: laravel where condition with if

$query = DB::table('user_ads')
            ->join('ads', 'users_ads.ad_id', '=', 'ads.id')
            ->orderBy($column, $method);

if ($input['search']) {
    $query->where('short_description', $input['search']);
}

if ($input['category']) {
    $query->where('category', $input['category']);
}

$query->join('users', 'users_ads.user_id', '=', 'users.id')
    ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')

$result= $query->get();

return $result;

Tags:

Php Example