How to Find Multiple Ids using Laravel Eloquent? (2022)

Hello dev, Today we are How to Find Multiple Ids using Laravel Eloquent? This tutorial will cover on finding multiple ids using eloquent in laravel application.

In this article, we will implement a laravel where multiple ids. we will help you to give an example of how to get data with multiple ids in laravel eloquent. you can understand a concept of how to find multiple ids in laravel eloquent. So, let’s follow a few steps to create an example of find multiple id laravel.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 versions.

If you want to get data with multiple ids in laravel then there are several ways to do it. Laravel eloquent provide find()findMany() and whereIn() to getting multiple ids data. so let’s see the example code bellow:

Also Read: Laravel Order By Multiple Columns Example (2022)

Examples for How to Find Multiple Ids using Laravel Eloquent

Example 1: Using find()

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select("*")
                    ->find([1, 2, 3]);
  
        dd($posts);
    }
}

Example 2: Using findMany()

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select("*")
                    ->findMany([1, 2, 3]);
  
        dd($posts);
    }
}

Also Read: How to Check If Collection is Empty in Laravel? (2022)

Example 3: Using whereIn()

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Post;
  
class PostController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(Request $request)
    {
        $posts = Post::select("*")
                    ->whereIn([1, 2, 3])
                    ->get();
  
        dd($posts);
    }
}

Related Question also Ask

  • How can I get multiple ID in laravel?
  • Is eloquent an ORM?
  • How do I create a search query in laravel?
  • Which types of relationships are available in laravel eloquent?

Conclusion

Today, We had learn How to Find Multiple Ids using Laravel Eloquent?. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Get Database Name in Laravel 2022?

3 Comments

Leave a Reply