2

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

3
  • please post your code, concat with eloquent you can do it with relation model, you need do some change in your migrations, and insert relation function in yout model User and Car Commented Nov 6, 2016 at 10:40
  • I did relation between Users and Cars , one to many , and it works fine with me with any queries Commented Nov 6, 2016 at 10:51
  • user hasMany , and car belongTo Commented Nov 6, 2016 at 10:52

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

I did some changes with '%$search%' which is '%'.$search.'%'
If you use double quotes then you can do that also as: "%$search%"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.