Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Concat Two Columns in Laravel Eloquent Query With Search

How to Concat Two Columns in Laravel Eloquent Query With Search ?

Aman Jain, June 22, 2022June 22, 2022

In this article we will learn to use aggregate function concat in laravel eloquent with search and query builder. Concat is used to join two columns and in laravel eloquent there is no method for concat but we can use DB builder to use native MySQL Methods. In MySQL or SQL based database we can do concat of columns using concat aggregate function.

Sometimes in our application we want to use join first name and last name or any other columns to show jointly in our application so we can either use mysql concat function or laravel accessor mutators.

Using the laravel

Concat mysql concates two or more column and result as one column. For example if you have first name, middle name and last name then you can concat three columns in one as full name and then we can search full name with any query.

Here is the syntax

Model::select(\DB::raw("concat(column_name," ",column_name_2) as full_name"))

//or

Model::where(\DB::raw("concat(column_name," ",column_name_2)"),"like","%query%")->get(); // or first() 

//or

\DB::table('table_name')->select(\DB::raw("concat(column_name," ",column_name_2) as full_name"))

In the above syntax we have used 3 examples to show all possibilities to use concat aggregate function in laravel.

First is using the Model and concat mysql function using DB class. Second is Model with where and Db::raw class to search the columns.

Result of above query will be:

select concat(column_name," ",column_name_2) as full_name from  `table`  

//OR

select * from `table` where concat(column_name," ",column_name_2) like "%query%"
 

Let’s understand laravel min in eloquent query with example

Example 1 – Mysql concat aggregate function query using eloquent

So in this example i will use mysql concat method in laravel eloquent to concat two columns.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select(\DB::raw("concat(first_name," ",last_name) as full_name"));
       \\or
       DB::table('articles')->select(\DB::raw("concat(first_name," ",last_name) as full_name"));
   }
}

Output will be :

select concat(first_name," ",last_name) as full_name from `articles`

and Use in View:

Use:

@foreach ($articles as $article)    {
   {{ $article->full_name }}
}
@endforeach

Example 2 – Mysql Concat with search in laravel eloquent

In this example we will search the concated columns using where

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::select(\DB::raw("concat(first_name," ",last_name) as full_name"))
             where(\DB::raw("concat(first_name," ",last_name)"),"like","%a%")->get();
        return view("home",["articles"=>$Article])
    }
}

Output will be :

select concat(first_name," ",last_name) as full_name from `articles` where concat(first_name," ",last_name) like '%a%'

Example 3 – Laravel Model accessor to concat the columns

In this example we will use Laravel Model accessor to concat the columns but this will not work with the search

<?php
  
namespace App\Models;
use Illuminate\Database\Eloquent\Model;

use Illuminate\Notifications\Notifiable; 
  
class Article extends Model {
    use Notifiable;
    
    public function getFullNameAttribute()
    {
        return "{$this->first_name} {$this->last_name}";
    }
  
}
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $Article = Article::all();
        return view("home",["articles"=>$Article])
    }
}

then We can use as follow :

@foreach ($articles as $article)    {
   {{ $article->full_name }}
}
@endforeach

Also Read : Laravel max in eloquent query with example

Related

Php Laravel concatlaravelmysql

Post navigation

Previous post
Next post

Related Posts

Laravel Get Today Records in Laravel

How to Get Today Records in Laravel ?

November 17, 2023March 16, 2024

In this blog post we will learn to get today records in laravel. we will use carbon and whereDate method of laravel eloquent to fetch the today records. In laravel models save the data in column created_at and created_at column stores the date time but if we want to fetch…

Read More
Devops How to install PHP

How to install PHP on Ubuntu?

September 18, 2021October 4, 2022

PHP is widely used langauge in world of web. PHP is an open sourece language and free for everyone. It’s a server side scripting lanaguge which compiles on server and can embedded with html too. Currently, there is three version are available of PHP that are as below PHP 5…

Read More
Php Use Soft Delete to Temporary (Trash) Delete the Records in Laravel

Use Soft Delete to Temporary (Trash) Delete the Records in Laravel ?

June 17, 2022June 17, 2022

In laravel we use Soft Delete to Temporary (Trash) Delete the Records. Soft delete in laravel is use to hide the record from users by keeping the data in the database. There is many purpose of soft delete like deleting the user for admin but keep all the information in…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version