Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

Do you mean to echo them together ? You could do

 // User model
public function getFullName() {
    return $this->first_name . ' ' . $this->last_name;
}
Last updated 2 years ago.
0

eriktisme said:

Do you mean to echo them together ? You could do

// User model

public function getFullName() { return $this->first_name . ' ' . $this->last_name; }

You forgot part of the accessor method name 'Attribute':

public function getFullNameAttribute() {
        return $this->first_name . ' ' . $this->last_name;
}

And to answer the question - if you want to query you can concatenate the fields in select or search through both first_name and last_name

Last updated 2 years ago.

geoffreyvanwyk liked this reply

1
$User::where('firstname', $firstname)->where('lastname', $lastname)->get();
Last updated 2 years ago.
0

The problem is that the user does not want to break apart the firstname & last name when querying. This is what I tried, but it does not work.Note I am hardcoding the fullname, I know I need to do something different to pass it in, but first I want to get this step below to work.

 $test=function($tid){
            $user = DB::table('users')
                ->select(DB::raw('CONCAT_WS(" ",`firstname`,`lastname`) as `wholename`,id'))
                ->where('CONCAT_WS(" ",`firstname`,`lastname`)','LIKE','John Smith')->first()
            ;
            return $user;
        };

        $u=$test($query);

        dd($u);

The error returned is....

Illuminate \ Database \ QueryException SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'firstname,lastname) LIKE ? limit 1' at line 1 (SQL: select CONCAT_WS(" ",firstname,lastname) as wholename,id from users where CONCAT_WS(" ",firstname,lastname) LIKE John Smith limit 1)

Last updated 2 years ago.
0

Although I am not happy with it, as a workaround I have added fullname to my user table. Hopefully, a better solution will emerge in the future.

Again, the problem was not with returning fullname, it was searching by fullname when the user table only contains firstname and lastname columns.

Last updated 2 years ago.
0

The problem here lies in the MySQL (correct me if you use other db), because you can't reference derived column in where clause. It will work with having clause, but mind that this query will process all the rows first.

Depending on your table size it might be better to use that additional column I suppose, but for small table you can use this:

DB::table('users')
  ->select(DB::raw('CONCAT_WS(" ",`firstname`,`lastname`) as `wholename`,id'))
  ->having('wholename', 'LIKE','John Smith')
  ->first();

cjjsjr5656 said:

$test=function($tid){
           $user = DB::table('users')
               ->select(DB::raw('CONCAT_WS(" ",`firstname`,`lastname`) as `wholename`,id'))
               ->where('CONCAT_WS(" ",`firstname`,`lastname`)','LIKE','John Smith')->first()
           ;
           return $user;
       };
       $u=$test($query);
       dd($u);

Last updated 2 years ago.
0

Thanks jarektkaczyk, I am marking yours as the solution.

Last updated 2 years ago.
0

I know this is an old thread, but you can achieve this quite easily with

$user = User::whereRaw('CONCAT(firstname, " ", lastname) LIKE ? ', '%' . $request->input('name') . '%');
0

Sign in to participate in this thread!

Eventy

Your banner here too?

cjjsjr5656 cjjsjr5656 Joined 14 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.