Call MySQL Stored Procedure in Laravel 8 Tutorial

Share this Article

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 How to run/call MySQL stored procedure in laravel 8. 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 8, all things will be cleared in this article.

Let’s get started.


Installation of Laravel 8 Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Click here to Install Composer ? Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel 8 application into system. To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel 8 already installed at 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

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 this code into your web.php file,

# Add to header
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.

<?php

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>";
        print_r($blogs);
    }

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

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

Application Testing

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

$ php artisan serve

URL To List All Blogs – http://127.0.0.1:8000/list-blog

URL To Single Blog – http://127.0.0.1:8000/single-blog/3

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

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.

Find More on Laravel 8 Articles here

Leave a Comment

What are CSRF Functions in CodeIgniter 4 Tutorial
MySQL Stored Procedure in CodeIgniter 4 Tutorial