How to Get Random Records in Laravel

How to Get Random Records in Laravel

Laravel get random records. In this tutorial, you will learn how to get random data from the database or collection in laravel using inRandomOrder().

Laravel get a random record example. Here you will learn how to get random records from DB in laravel using inRandomOrder() method.

You must be visiting many blogs. So sometimes you have seen these blogs in the sidebar. It is written random posts and there are some posts below it.

If you are building a blog application in laravel framework. And want to get random posts/data from the database in laravel. And want to display this random data anywhere on your laravel blog. So at that time, you need to use inRandomOrder() method to get random records in laravel.

This article will guide you on how to use get random records from the database in laravel using inRandomOrder() method. As well as get random records from collection in laravel.

Note that, Laravel inRandomOrder method may be used to sort the query results randomly. For example, if you want to get random posts from DB in laravel. So you can use the below following methods as well.

1: Laravel Retrieve Random Records From DB with Eloquent

The following example will fetch the 5 random posts from DB table in laravel:

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $data = DB::table('posts')
                ->inRandomOrder()
                ->limit(5)
                ->get();
}

Recommended:- Laravel Where Null and Where Not Null Query

2: Laravel Retrieve Random Records From DB using Model

The following method also will fetch the 5 random posts from DB table in laravel:

/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
    $data = Post::inRandomOrder()
                ->limit(5)
                ->get();
}

Recommended:- Laravel Eloquent whereRaw Query Example

Conclusion

In this laravel fetch random records from database or collection, you have learned how to get random records from database and collection using inRandomOrder() method in laravel framework.

laravel laravel inrandomorder() query example

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to validate mobile phone number in laravel with example

Data validation is a very important thing for a web application. In this article i will let you know how to validate mobile phone number in laravel.

Laravel 8 Livewire Form Wizard Tutorial Example

Form wizard in laravel 8 app with livewire. In this tutorial, you will learn how to implement form wizard using livewire in laravel 8 app.

How to integrate paytm payment gateway in laravel example

In this article, i will let you know to integrate paytm payment gateway in laravel with example. In this example will use paytm laravel package.

Laravel 8 Form Example Tutorial - Complete Guide

Laravel 8 form example tutorial. In this post, i will show you how to send form data on controller and insert in database using laravel 8

Laravel 7/6 Image Upload Example Tutorial

Laravel 7/6 image upload example tutorial. Here we will show you, how to upload image in laravel 7/6 with preview and validation in laravel.