The query did not execute properly

Viewed 78
$data = User::where('inet_id', "like", "%" . $inet . "%")
         ->orWhere('email', "like", "%" . $email . "%")
         ->orWhere('phone', "like", "%" . $phone . "%")
         ->get();
  1. It only executes the query when searching all 3 data once. But I want to search by separate data too.
1 Answers

I think you can use when

$data = User::where(function($q)use($inet,$email,$phone){
      $q->when(!empty($inet), function ($query) use($inet) {
            $query->where('inet_id', 'like', "%{$inet}%");
         })->when(!empty($email), function ($query) use ($email) {
            $query->orWhere('email', 'like', "%{$email}%");
         })->when(!empty($phone), function ($query) use ($phone) {
            $query->orWhere('phone', 'like', "%{$phone}%");
      })
    })->get();
Related