sql concatenate two columns – How to Concat First Name and Last Name in Laravel?

Rate this post

sql concatenate two columns with Concat First Name and Last Name in laravel db select query with Controller Code bellow.

sql concatenate two columns

you are trying to separate the first name and last name from a full name, currently using the following laravel source code.

Example 1: Concat First Name and Last Name

Laravel Controller Code:

<?php
  
namespace App\Http\Controllers;
  
use App\Models\Member;
use DB;
  
class PakaController extends Controller
{
    /**
     * Write code on Method
     * created by Pakainfo.com
     * @return response()
     */
    public function index()
    {
        $members = Member::select("id", DB::raw("CONCAT(first_name, ' ', last_name) as full_name"))
                    ->get();
  
        dd($members);
    }
}

Result

Array
(
    [0] => Array
        (
            [id] => 126
            [full_name] => Ridham Bhanderi
        )
    [1] => Array
        (
            [id] => 127
            [full_name] => Ridham Kakadiya
        )
)

Don’t miss : laravel search query

Read Also:  jQuery Ajax Image Upload without Page Refresh using PHP

Example 2: Concat First Name, Middle Name and Last Name

Laravel Controller Code:

<?php
  
namespace App\Http\Controllers;
  
use App\Models\Member;
use DB;
  
class PakaController extends Controller
{
    /**
     * Write code on Method
     * created by Pakainfo.com
     * @return response()
     */
    public function index()
    {
        $members = Member::select("id", DB::raw("CONCAT(first_name, ' ', middle_name, ' ', last_name) as full_name"))
                    ->get();
  
        dd($members);
    }
}

Result

Array
(
    [0] => Array
        (
            [id] => 126
            [full_name] => Ridham Karshanbhai Bhanderi
        )
    [1] => Array
        (
            [id] => 127
            [full_name] => Ridham Karshanbhai Kakadiya
        )
)

Free Live Chat for Any Issue
Download

I hope you get an idea about sql concatenate two columns.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.