Laravel 8 Eloquent withExists() Example


Share:
 | Published By: Admin | Published On: May 19, 2021 | Category: Laravel 8 Laravel

Hi Dev,

Today, I will learn you how to use laravel 8 exists query builder. i explained simply about laravel exists() example. you will learn how to check if record exists in laravel. i explained simply about laravel check if record exists in database.

You can check if records is exists or not in database in your laravel 6, laravel 7 and laravel 8 projects.

Laravel added eloquent methods exists() for check if record exists in database table or not. so i will give you very simple example so you don't need to use first() and then check is null or not. we will use direct exists() that will help you to determine if exist or not.

exists() methods return true or false value so you have to just put in your condition.

Here, I will show full example of exists() methods follow my step.

Example
>?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;
use Illuminate\Support\Facades\DB;


class ProductController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
      $is_exists = Product::select("*")->where("name","dell")->exists();

      if($is_exists){
        dd("Records is Available");
      }else{
        dd("Records is not Available");
      }
    }

}

Output
"Records is Available"

I Hope It will help you..

Share: