Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat columns using “with()” function in Laravel Eloquent

I'm trying to concat two columns from different tables into one single column.

$user = User::with(array('Person'=>function($query){
    $query->selectRaw('CONCAT(prefix_person.name, " - ", prefix_user.code) as name, prefix_user.id');
}))->lists('name', 'id');

In my person class I have this method:

public function User()
{
    return $this->hasOne('User');
}

And in my user class I have this one:

public function Person()
{
    return $this->belongsTo('Person', 'person_id');
}

I get the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'field list' (SQL: select `name`, `id` from `prefix_user`)

When I tried

$user = User::with(array('Person'=>function($query){
    $query->selectRaw('CONCAT(prefix_person.name, " - ", prefix_user.code) as name, prefix_user.id')->lists('name', 'id');
}));

I got this error:

error


I have used the selectRaw a couple of times, but never needed it into a with (join).

like image 712
Michel Ayres Avatar asked Apr 08 '15 19:04

Michel Ayres


People also ask

How do I merge two columns in Laravel?

both way you can easily concatenating two columns in laravel application. First way is we will concat two columns in select statement using db raw in laravel. Second way is we will concat two columns in select statement using pluck in laravel. Third one is model in use custom method to concat two column.

How do I concatenate a group in Laravel eloquent?

You can use relations as query builder to fetch the results as: $assignment_details = $assignment->raw_plan() ->select(DB::raw('group_concat(name) as names')) ->where('assignment_id', 1) ->groupBy('flag') ->get(); Use table_name. * in select to get all the fields.

What is selectRaw in Laravel?

You can use the laravel selectRaw eloquent method to building query in laravel apps. And also use laravel select raw with multiple conditions in eloquent queries. So, let's see following examples that will help you how to use selectRaw() eloquent query in laravel: Example 1: Laravel selectRaw Query using Model.


2 Answers

The issue is that Eloquent will first query the users table, and only after, the persons table, so one query is not aware of the other and thus concatenating will not work.

You can use the Query Builder to do this using a join. It will be something like it:

$user = DB::table('users as u')
    ->join('persons as p', 'p.id', '=', 'u.person_id')
    ->selectRaw('CONCAT(p.name, " - ", u.code) as concatname, u.id')
    ->lists('concatname', 'u.id');

EDIT: And, as suggested by @michel-ayres comment, as long as you have an acessor to the field:

public function getFullNameAttribute() { 
    return $this->attributes['name'] . ' - ' . $this->attributes['code'];
}

you can use your own model to perform the join and listing:

User::join('person','person.id','=','user.person_id')
    ->select('person.name', 'user.code', 'user.id')
    ->get()
    ->lists('full_name', 'id');
like image 100
Ravan Scafi Avatar answered Oct 20 '22 01:10

Ravan Scafi


You can solve it simply by using simple query,

User::join('persons as p', 'p.id', '=', 'users.person_id')
       ->get([
            'id',
            DB::raw('CONCAT(p.name,"-",users.code) as name')
        ])
       ->lists('name', 'id');

Or, see another way

User::join('persons as p', 'p.id', '=', 'users.person_id')
      ->select(
          'id',
          DB::raw('CONCAT(p.name,"-",users.code) as name')

        )
       ->lists('name', 'id');
like image 41
Majbah Habib Avatar answered Oct 20 '22 00:10

Majbah Habib