Laravel Eloquent Find by Column Name Example
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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 9 One to Many Eloquent Relationship Tutorial
- Laravel 9 Eloquent Mutators and Accessors Example
- Laravel Eloquent doesntHave() Condition Example
- Laravel Eloquent whereHas() Condition Example
- Laravel Eloquent When Condition Example
- Laravel Eloquent Model Custom Function Example
- Laravel Eloquent updateOrCreate Example
- Laravel Eloquent firstOrNew Example
- Laravel Eloquent whereNotNull() Query Example
- Laravel Eloquent selectRaw() Query Example
- Multiple orWhere Condition in Laravel Eloquent
- How to use Union Query with Laravel Eloquent?
- Laravel Has Many Through Eloquent Relationship Tutorial