Home > Software engineering >  Check entry exist in multiple table using laravel query builder
Check entry exist in multiple table using laravel query builder

Time:05-12

Is there any other way to written this code like laravel expert

$dncExist   =   DB::table('dnc_table1')
                   ->where('phone_number', '=', $phone)
                   ->get();

if(count($dncExist) == 0)
{
      $dncExist   =   DB::table('dnc_table2')
                         ->where('phone_number', '=', $phone)
                         ->get();
}

CodePudding user response:

    $dncExist   =   DB::table('dnc_table1')
                   ->where('phone_number', $phone)
                   ->first();

if(is_null($dncExist))
{
      $dncExist   =   DB::table('dnc_table2')
                         ->where('phone_number', $phone)
                         ->first();
}

CodePudding user response:

You can do it the following way.

if (!(YourMOdelName::where('phone_number', $phone )->exists())) {

  $dncExist   =   DB::table('dnc_table2')
                         ->where('phone_number', $phone)
                         ->first();
}

Or

    $dncExist   =  DB::table('dnc_table1')
                   ->where('phone_number', $phone)
                   ->first();

   if(!empty( dncExist )){

      $dncExist   =   DB::table('dnc_table2')
                         ->where('phone_number', $phone)
                         ->first();
}
  • Related