How To Find By Column Name in Laravel Eloquent Tutorial

Share this Article
Reading Time: 3 minutes
272 Views

Inside this article we will see the concept i.e How To Find By Column Name in Laravel Eloquent Tutorial. Article contains the classified information about searching database rows using column name in laravel eloquent.

If you are looking for a solution i.e find data from database using column wise conditions then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Example

Find data using where() method

User::where('name', 'Sanjay')->first();

Find data using column name

User::whereName('Sanjay')->first();

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Example #1: Find Row Using where() Method

Here, we will use simple method of eloquent i.e where() method.

Syntax

where("Column name", "Value")

Code Example

Suppose you want to get a user details with name “Siddharth Singh”, then it will be like

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $user = User::where('name', 'Siddharth Singh')->first();
        dd($user);
    }
  
}

Example #2: Find Row Using whereColumnName() Method

Here, we will use simple method of eloquent i.e whereColumnName() method.

Syntax

whereColumnName("Value")

Examples

Column Name: "name", then whereName("Value")

Column Name: "age", then whereAge("Value")

Column Name: "phone_number", then wherePhoneNumber("Value")

Code Example

Suppose you want to get a user details with name “Siddharth Singh”, then it will be like

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $user = User::whereName('Siddharth Singh')->first();
        dd($user);
    }
  
}

We hope this article helped you to learn How To Find By Column Name in Laravel Eloquent Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4