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

Php Google Map with Multiple Marker and Info Box in Laravel

Google Map with Multiple Marker and Info Box in Laravel

August 30, 2022March 16, 2024

In this article we will learn to integrate google map with multiple marker and info box in laravel. Google maps are used to navigate the user location or indicate the location of business and any user. Sometimes In our application we want to show the multiple location with some information…

Read More
Php How to create custom logs file in laravel 8 9

How to create custom logs file in laravel 8 / 9 ?

May 16, 2022May 16, 2022

In this article i will show you to use and create custom logs file in laravel. Logging is an important aspect when you want to debug your application or you want to monitor the user activities on the application. Laravel itself provides a robust library to create logging on daily…

Read More
Php How to Get Current Url with Query Params in Laravel

How to Get Current Url with Query Parameters in Laravel ?

August 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url with Query Parameters in Laravel. It can be complete url, route name and query params too so that we can fetch the data accordingly and show to user on basis of conditions in our view or controller. We can easily…

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

  • 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

  • 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
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version