skip to Main Content

I’m trying to filter account in laravel.1 user can have many accounts with different format. It doesnt seem to work.The query:

$accounts = $user->account()->where('account_no','like', ['34%','35%','3303%','3304%'])->get();  

i’m wondering if this can be done or not?

3

Answers


  1. You can’t do that. For each condition you need a single WHERE condition, like this:

    $accounts = $user->account()->where('account_no','like','34%')
      ->orWhere('account_no','like','35%')
      ->orWhere('account_no','like','3303%')
      ->orWhere('account_no','like','3304%')
      ->get();
    

    That should do it.

    Login or Signup to reply.
  2. You can try this workaround :

    $checks = array('34','35','3304','3305');  
    
    $accounts = $user->accounts()            
        ->Where(function ($query) use($checks) {
             foreach ($checks as $check){
                $query->orwhere('account_no', 'like', $check .'%');
             }      
        })->get();
    
    Login or Signup to reply.
  3. Try it by RAW Query,
    Like this:

    $accounts = $user->account()->whereRaw("account_no like ('34%','35%','3303%','3304%') ")->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search