How to Find Multiple Ids using Laravel Eloquent?
Hello,
Are you looking for an example of laravel eloquent find multiple ids. 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, laravel 9, laravel 10 and laravel 11 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:
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);
}
}
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);
}
}
I hope it can help you...