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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Check If Collection is Empty in Laravel?
- Laravel Create Record if Not Exists Example
- How to Set Default Value in Laravel Model?
- How to Get Last 10 Records in Laravel?
- Laravel Eloquent firstOrCreate Example
- Laravel Eloquent Delete Record By ID Example
- Delete All Records from Table in Laravel Eloquent
- Laravel Eloquent whereBetween() Query Example
- Laravel One to One Eloquent Relationship Tutorial
- Laravel One to Many Eloquent Relationship Tutorial
- Laravel Many to Many Eloquent Relationship Tutorial
- How to Get Query Log in Laravel Eloquent?