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 How to change data type of column in laravel migration

How to change data type of column in laravel migration ?

February 20, 2022November 5, 2023

In this blog post we will learn to change data type of column in laravel migration. Laravel covers most of the migration features like add, delete, indexing etc. but to modify the table like renaming column, change data type column to existing table laravel uses a separate package doctrine/dbal. In…

Read More
Php Get Json Post Data in Laravel

How to Get Json Post Data in laravel from Request ?

June 8, 2022February 8, 2024

In this article we will learn to get json post data in laravel from request. Laravel by default supports for form-data and x-www-form-urlencode which we can get easily using the Request class as follow $request->field_name or $request->get(‘field_name’). To retrieve the json post data in laravel we need to call json…

Read More
Php How to Show Success and Error Flash Message in Laravel

How to Show Success and Error Flash Message in Laravel 10 ?

March 26, 2023March 16, 2024

This article aims to teach how to display flash messages indicating success or error in Laravel 10. Flash messages come in handy when notifying users of recent activities such as form submissions or website actions. In such cases, multiple types of messages may be required. Sometimes, error messages need to…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version