Laravel 9 How to get Random Database Records Tutorial

Share this Article
Reading Time: 3 minutes
439 Views

Inside this article we will see the concept i.e Laravel 9 How to get Random Database Records Tutorial. Article contains the classified information about How to get random records in laravel.

If you are looking for a solution i.e How to get random database records in laravel then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Database Table Rows

Suppose we have a table called posts. In this table these are test rows inserted.

Next,

We need to get random data rows from this posts table.

Create Controller

Back to project terminal and run this command to create a controller file.

$ php artisan make:controller SiteController

Above command will create a file i.e SiteController.php inside /app/Http/Controllers folder.

Open SiteController.php and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SiteController extends Controller
{
    public function index()
    {
        $data = DB::table('posts')
            ->select('posts.id', 'posts.title', 'posts.body')
            ->inRandomOrder()
            ->limit(5)
            ->get();
        dd($data->all());
    }
}

Concept

We have used ->inRandomOrder() to get random database rows from posts table.

Create Route

Open web.php file from /routes folder. Add this route into it.

//...

use App\Http\Controllers\SiteController;

Route::get("getdata", [SiteController::class, "index"]);

//...

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL: http://127.0.0.1:8000/getdata

We hope this article helped you to learn Laravel 9 How to get Random Database Records Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4