laravel search query – How to Search First Name and Last Name in Laravel Query?

Rate this post

Laravel search query with multiple conditions add simple “where” clauses to your search queries Example with Like Operator.

laravel search query

The database table stores firstname and lastname as separate fields. the following tables write a SQL query to find the First Name and Last Name.

Example 1:

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()
    {
        $search = "Ridham Ka";
        $members = Member::select("id", "first_name", "last_name")
                       ->orWhere(DB::raw("concat(first_name, ' ', last_name)"), 'LIKE', "%".$search."%")
                       ->get();
  
        dd($members);
    }
}

Result

Array
(
    [0] => Array
        (
            [id] => 127
            [first_name] => Ridham
            [last_name] => Kakadiya
        )
)

Don’t miss : laravel group by

Read Also:  Laravel 5.6 Dependent Dynamic dropdown Example Tutorial

Example 2 :

Laravel Controller Code:

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

Result

Array
(
    [0] => Array
        (
            [id] => 127
            [first_name] => Ridham
            [last_name] => Kakadiya
        )
)

Example 3:

Laravel Controller Code:

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

Free Live Chat for Any Issue

Result

Array
(
    [0] => Array
        (
            [id] => 127
            [first_name] => Ridham
            [last_name] => Kakadiya
        )
)

Download

I hope you get an idea about laravel search query.
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.