How to get only the data with a specific Id in Laravel?

Today, I will give you an example of “How to get only the data with a specific Id record in Laravel”, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

whereIn Method in Laravel

The whereIn method verifies that a given column’s value is contained within the given array:

Syntax:

$products = Products::whereIn('cat_id', [1, 2, 3])->get();

whereNotIn Method in Laravel

The whereNotIn method verifies that the given column’s value is not contained in the given array:

Syntax:

$products = Products::whereNotIn('cat_id', [1, 2, 3])->get();

Let’s understand the whereIn method with the example below :

We have a products table in the database and a column cat_id, we use whereIn and whereNotIn methods in laravel to get specific category ids data.

Products Table Data

product table data with category id
product table data in blade file

routes\web.php

Route::get('/list-product','ProductController@ListProduct');

Now we will get only cat_id 1(apple) data using the whereIn Method in Controller.

app\Http\Controllers\ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;


class ProductController extends Controller
{


    public function ListProduct()
    {
        $products = Product::whereIn('cat_id',[1])->get();
        return view('product.list',compact('products'));
    }
}
whereIn in laravel

resources\views\product\list.blade.php

 <div class="container">
         <h3>Product List Data</h3>
         <table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Product Name</th>
                  <th>Product Price</th>
                  <th>Product Short Description</th>
               </tr>
            </thead>
            <tbody>
               @foreach($products as $product)
               <tr>
                  <td>{{ $product->id }}</td>
                  <td>{{ $product->name}}</td>
                  <td>{{ $product->price}}</td>
                  <td>{{ $product->short_description }}</td>
               </tr>
            </tbody>
            @endforeach
         </table>
  </div>

whereIn use in array in laravel

Now we will get only cat_id 2(Samsung) data using the whereNotIn Method in Controller.

$products = Product::whereNotIn('cat_id', [1,3])->get();
get a specific id record in laravel

If you want to use the whereIn method Directly in the Blade file in the laravel application.

    <table class="table">
       <thead>
           <tr>
              <th>S.no</th>
              <th>Product Name</th>
              <th>Product Price</th>
              <th>Product Short Description</th>
            </tr>
            </thead>
            <tbody>
            @foreach($products->whereIn('cat_id',[1]) as $product)
            <tr>
              <td>{{ $product->id }}</td>
              <td>{{ $product->name}} </td>
               <td>{{ $product->price}}</td>
               <td>{{ $product->short_description }}</td>
            </tr>
            </tbody>
            @endforeach
     </table>
whereIn use in array in laravel

In this article, we learned How to get only the data with a specific Id record in Laravel and Difference between the wherIn and whereNotIn Methods in Laravel”, I hope this article will help you with your Laravel application Project.

Read also: Send email to multiple users using Queue in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top