Laravel Eloquent Find by Column Name Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi Guys,

In this example, I will show you how to find records by column name in laravel eloquent.

You can use these tips with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

By default, laravel provides find() method to find records from the database. with find() method laravel will compare with id column, But if you want to check with another column like name, title, etc. then how you will find by column name? I will show you the below simple examples to find records by column name.

Example 1:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$user = User::where('name', 'Hardik Savani')->first();

dd($user);

}

}

Example 2:

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$user = User::whereName('Hardik Savani')->first();

dd($user);

}

}

I hope it can help you...

Tags :
Shares