logo
down
shadow

Laravel Eloquent: Add filter in where (if-else for Conditional Queries)?


Laravel Eloquent: Add filter in where (if-else for Conditional Queries)?

By : user2185073
Date : November 20 2020, 04:01 AM
Hope this helps I have this code : , First pass the query to a variable and update the query variable.
code :
$query = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name')
      ->take(3);

if( isset($_POST['brand'])) {
    $query->where('p.brand', $_POST['brand'])
}
if (isset($_POST['color'])) {
    $query->where('p.color', $_POST['color'])
}
if (isset($_POST['product_type'])) {
    $query->where('p.product_type', $_POST['product_type'])
}

$result = $query->get();
$result = DB::table('product as p')
      ->join('master as cl', 'p.color', '=', 'cl.name')
      ->join('master as brand', 'p.brand', '=', 'brand.name')
      ->where('p.brand', $brandname)
      ->whereRaw("p.del_flag = 0  and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
      ->when(isset($_POST['brand']), function ($query) {
            $query->where('p.brand', $_POST['brand']);
      })
      ->when(isset($_POST['color']), function ($query) {
            $query->where('p.color', $_POST['color']);
      })
      ->when(isset($_POST['product_type']), function ($query) {
            $query->where('p.product_type', $_POST['product_type']);
      })
      ->select('p.name')
      ->groupBy('p.name')
      ->orderBy('p.name')
      ->take(3)
      ->get();


Share : facebook icon twitter icon
Eloquent conditional where filter

Eloquent conditional where filter


By : Roberto Tirado Varel
Date : March 29 2020, 07:55 AM
I hope this helps you . You could use a dynamic scope
code :
class User extends Eloquent {

    public function scopeGender($query, $gender)
    {
        if ($gender) {
           return $query->whereGender($gender);
        }
        return $query;
    }
}
...->gender($gender)->get();
Laravel Eloquent ORM: Building nested conditional queries

Laravel Eloquent ORM: Building nested conditional queries


By : Rachael
Date : March 29 2020, 07:55 AM
it helps some times I have 5 user titles, defined by booleans: CEO, executive, manager, employee & intern. , Maybe you can do something like this ? (with use function keyword)
code :
<?php

$filters = ['manager', 'employee'];

$users = User::where(function($query) use($filters) {
   foreach( $filters as $filter )
      $query = $query->orWhere($filter);
})
->where([ADDITIONAL CONSTRAINTS... (like age)])->get();
Entrust with Laravel - Eloquent queries with conditional roles

Entrust with Laravel - Eloquent queries with conditional roles


By : M. Ferriere
Date : March 29 2020, 07:55 AM
it fixes the issue This is how I managed to make it work. However I'm not able to get the Relation object instead I'm getting collection of Users having role Admin
code :
public function admin(){

    return $this->hasOne('App\User')->get()->map(function($u){

        if($u->hasRole('admin') && $u != null){

            return $u;

        }

    })->reject(function ($u){
        return $u == null;
    });


}
Laravel MySQL: translating SQL queries to Laravel Eloquent

Laravel MySQL: translating SQL queries to Laravel Eloquent


By : jksw
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to get time availability for my scheduling system. I know how to do it with plain SQL query but I am having trouble translating the query to Laravel Eloquent. My query is like the one below , You need to call the variables to closure scope by using use
code :
$sch = DB::table('schedules')
                ->where(function ($query) use ( $start_datetime,$end_datetime) {
                    $query->whereBetween('time_start', [$start_datetime, $end_datetime]);
                })
                ->orWhere(function($query) use ( $start_datetime,$end_datetime){
                    $query->whereBetween('time_end', [$start_datetime, $end_datetime]);
                })->get();
Laravel eloquent, joining tables and make filter queries

Laravel eloquent, joining tables and make filter queries


By : user3448413
Date : March 29 2020, 07:55 AM
To fix the issue you can do I am trying to write queries to filtering tables, but it's looks like something is not right. , You have two problems:
code :
->when($keyword, function($query) use ($keyword){
        $query->with(["basicInfos" => function($query) use ($keyword){
            return $query->where("wedding_style", "LIKE", "%$keyword%");
        }]);
    })
Related Posts Related Posts :
  • Why Laravel `bcrypt` or Hashing a random string password is mismatched when checking?
  • Check from_date before to_date and to_date after from_date, also both dates before today in laravel nova validation
  • Customizing Method Not Allowed, 404
  • Laravel 5.7 Multi Authentication
  • I want to use pagination with laravel Raw query builder
  • Laravel whereHas behaving unexpectedly
  • only one value is get use of explode variable in get method
  • laravel 5.7 console test how to create Artisan::command()
  • Laravel transaction return null
  • Laravel Eloquent how to override the base query?
  • Trying to retrieve posts from table by category slug
  • Laravel return value of a specific column in a relationship
  • Error class initialization in the provider?
  • Sending a verification email when registring from the api
  • i have an error when use chart on laravel
  • How to handle external API rate limits?
  • How to use telescope with moloquent / mongodb?
  • return redirect() is not working after a failed validation
  • Trying to add items added to cart to navbar
  • How to get (parse) this JSON in Laravel?
  • How to deploy laravel into a docker container while there are jobs running
  • Laravel - Avoiding code duplication in models
  • Custom pivot model class does not exist in controller
  • How to adjust card height with Bootstrap in Laravel 5
  • Can eloquent ignore irrelevant data in Laravel 5.7
  • Vue API Calls and Laravel Middleware
  • Laravel Nova not loading any resources, blade error
  • Laravel Backpack how to bounce to exact match instead of showing just 1 result in table
  • Laravel intermittent logout
  • Laravel Relationship Find UUID
  • Is it safe to use DB :: select and others in Laravel?
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org