Do you mean to echo them together ? You could do
// User model
public function getFullName() {
return $this->first_name . ' ' . $this->last_name;
}
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
geoffreyvanwyk liked this reply
$User::where('firstname', $firstname)->where('lastname', $lastname)->get();
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)
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.
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);
Thanks jarektkaczyk, I am marking yours as the solution.
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') . '%');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community