ItSolutionStuff.com

How to Check If Record Exists or Not in Laravel?

By Hardik Savani β€’ November 5, 2023
Laravel

Today, i will let you know example of laravel check if record exists in database. We will look at example of laravel check if record not exists. This post will give you simple example of laravel check if record exists in table. let’s discuss about laravel check if record exists in database. So, let's follow few step to create example of laravel check if record not exists.

If you are new in laravel and you want to determine email is exist or not then you can do it easily. IF you did use code php then we have to write long code and check but laravel provide it's own query builder so it is pretty easy. you can see following example how i check row is exists or not:

using first() Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$user = User::where('email',Input::get('email'))->first();

if (!is_null($user)) {

dd('Record is available.');

}else{

dd('Record is not available.');

}

}

}

Output:

Record is available.

using exists() Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$isExist = User::select("*")

->where("email", "yemmerich@example.net")

->exists();

if ($isExist) {

dd('Record is available.');

}else{

dd('Record is not available.');

}

}

}

Output:

Record is available.

using doesntExist() Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$isExist = User::select("*")

->where("email", "yemmerich@example.net")

->doesntExist();

if ($isExist) {

dd('Record is not available.');

}else{

dd('Record is available.');

}

}

}

Output:

Record is not available.

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

How to Group By with Order By Desc in Laravel?

Read Now β†’
β˜…

Laravel Order By Relation Column Example

Read Now β†’
β˜…

Laravel Multiple Where Condition Example

Read Now β†’
β˜…

Laravel Eloquent WhereNotIn Query Example

Read Now β†’
β˜…

Laravel Order By Relationship Sum Column Example

Read Now β†’
β˜…

Laravel One to Many Eloquent Relationship Tutorial

Read Now β†’
β˜…

Laravel WhereIn() and WhereNotIn() with Subquery Example

Read Now β†’
β˜…

Laravel Where Clause with date_format() Example

Read Now β†’
β˜…

Laravel Eloquent Order By Random Row Example

Read Now β†’
β˜…

Laravel Eloquent Where Like Query Example Tutorial

Read Now β†’
β˜…

Laravel Repository Pattern Tutorial Example

Read Now β†’
β˜…

Laravel Where Clause with MySQL Function Example

Read Now β†’
β˜…

How to Get Query Log in Laravel Eloquent?

Read Now β†’