Laravel 9 Call MySQL Stored Procedure Tutorial

Reading Time: 5 minutes

A procedure (often called a stored procedure) is a collection of pre-compiled SQL statements stored inside the database. It is a subroutine or a subprogram in the regular computing language.

Inside this article we will see the concept of Laravel 9 Call MySQL Stored Procedure. This article will be very interesting to know and learn. We will see the complete guide of mysql stored procedure creation and calling from a laravel application.

How to use a mysql stored procedure in laravel 9, all things will be cleared in this article.

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


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;

Successfully, we have created a database.

To connect, Open up .env file which will be at root of project.


Successfully, we have connected database to application.

Create MySQL Stored Procedure

Open MySQL database.

Let’s say we have a table in database called as blogs. In this table we have 100 rows. By the help of procedure, we will read all blogs as well as any specific blog.

We will create stored procedure by using phpmyadmin manual interface option to add routines.

We will create two MySQL stored procedure here for this article.

  • Read all blogs from table
  • Read a Single Blog from Table

Read all blogs from table

Read a Single Blog from Table

Successfully, we have created two mysql stored procedures in database.

Create Routes

Open web.php from /routes folder. Inside this file we will add few routes which calls methods of controller.

Add these routes into your web.php file,


use App\Http\Controllers\DataController;

Route::get("list-blog", [DataController::class, "listBlogs"]);
Route::get("single-blog/{id}", [DataController::class, "singleBlog"]);


Create Controller

Open project into terminal and run this artisan command to create controller.

$ php artisan make:controller DataController

It will create DataController.php file at /app/Http/Controllers folder.

Open DataController.php and write this following code.


namespace App\Http\Controllers;

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

class DataController extends Controller
    public function listBlogs()
        $blogs = DB::select("CALL getBlogs()");

        echo "<pre>";

    public function singleBlog($id)
        $single_blog = DB::select("CALL getSingleBlogDetail(" . $id . ")");

        echo "<pre>";
  • Using CALL getBlogs() to call, list all blogs
  • Using CALL getSingleBlogDetail(” . $id . “) to get specific blog detail.

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL To List All Blogs –

URL To Single Blog –

We hope this article helped you to learn about Laravel 9 Call MySQL Stored Procedure Tutorial in a very detailed way.

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more