phplaravellaravel-5eloquent

Using CONCAT with Eloquent in Laravel


I want to use concat with eloquent , I have two tables (users) and (cars) From users table I want to use first_name , last_name fields, and from cars table I want to use field called (vin).

$cars   = Car::where(DB::raw('concat(first_name," ",vin," ",make)') , 'LIKE' , '%$search%')->paginate();

first_name field from Users table and the rest from Cars table


Solution

  • You can use join() for this as:

    $cars   = Car::join('users', 'cars.user_id', '=', 'users.id')
                ->where(DB::raw('concat(users.first_name," ",cars.vin," ",cars.make)') , 'LIKE' , "%$search%")
                ->select('cars.*')
                ->paginate();