PHPackages                             jlw/laravel-stored-procedure - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jlw/laravel-stored-procedure

ActiveLibrary[Database &amp; ORM](/categories/database)

jlw/laravel-stored-procedure
============================

Add a proc() method to Eloquent Query Builder for calling stored procedures.

00PHPCI failing

Since Jun 8Pushed 11mo agoCompare

[ Source](https://github.com/james-waring/laravel-stored-procedure)[ Packagist](https://packagist.org/packages/jlw/laravel-stored-procedure)[ RSS](/packages/jlw-laravel-stored-procedure/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Latest Version on Packagist](https://camo.githubusercontent.com/665b23dc08eedac7d311d7c238e599ba794a19f3bf3c41a17293960581ca6441/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6c772f6c61726176656c2d73746f7265642d70726f6365647572652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jlw/laravel-stored-procedure) [![Total Downloads](https://camo.githubusercontent.com/776aae377b4d538119309ab794aa2cf9d5188ceeb05a9cb8610838f6bb6c3968/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6c772f6c61726176656c2d73746f7265642d70726f6365647572652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jlw/laravel-stored-procedure)

Laravel Stored Procedure
------------------------

[](#laravel-stored-procedure)

A Laravel package for calling **MySQL/MariaDB stored procedures** with optional **pagination** and **model hydration**.

- Call **single** or **multi-result set** stored procedures
- **Pagination** using Laravel’s paginator for the first result set
- Optionally **hydrate models** on multiple result sets

### Basic calls

[](#basic-calls)

```
// Simple call to a procedure that returns a single result set:
$result = TestModel::CallStoredProcedure('your_procedure_name', [$param_1])
    ->get();

// Simple call to a procedure that returns a single result set,
// paginated with the default per-page as 15:
$result_paginated = User::CallStoredProcedure(
    'your_procedure_name',
    [$param_1, $param_2, $param_3]
)
->paginate();

// If you want to set a custom per-page, you can do so like this:
$result_paginated = User::CallStoredProcedure('your_procedure_name')
    ->paginate(100);

// If your procedure returns multiple result sets
// you can set the total_result_sets parameter:
$result_sets = User::CallStoredProcedure(
    procedure_name: 'your_procedure_name',
    parameters: [$param_1, $param_2],
    total_result_sets: 3
)
->get();
```

Hydrating multiple result sets
------------------------------

[](#hydrating-multiple-result-sets)

This example shows how to hydrate multiple result sets. Simply pass the number of result sets you expect to the `CallStoredProcedure` method then pass the model classes you want to hydrate to the `setHydrateModels` method. The first results set is excluded from the hydrate models method as it will be hydrated by the model that called the stored procedure.

```
$result_sets = User::CallStoredProcedure('your_procedure_name', [$param_1, $param_2], 5)
    ->setHydrateModels([Token::class, Subscription::class, null, Plan::class])
    ->paginate();
```

The first result set is hydrated by the `User` model, the second result set is hydrated by the `Token` model, the third result set is hydrated by the `Subscription` model, the fourth result set is not hydrated and will return an array of objects, and the fifth result set is hydrated by the `Plan` model.

Why did i make this?
--------------------

[](#why-did-i-make-this)

This all started when i was working on a project that required a complicated query that was too complex to be done in a single query. I first tried writing the query with CTE's but they ended up being to slow. I began to wonder if a stored procedure would be a better solution. This procedure ended up having multiple temporary tables and multiple result sets. Once i had the stored procedure done it was returning the results sets in nano seconds where as the normal query with a CTE's was taking 5 seconds or more.

So the only issue now was how to call the stored procedure and get the results back into my application. I ended up writing a package that would allow me to call stored procedures with laravel pagination and model hydration for the results sets.

### So you probably want to know how this works under the hood?

[](#so-you-probably-want-to-know-how-this-works-under-the-hood)

If you are just doing a `get()` then there is nothing more you need to do in your stored procedure.

If you are doing a `paginate()` you will need to do a few things first. The first thing you will need to do is add two `IN` parameters to your stored procedure, one for the per page and one for the page number. Then you will need to select the total number of rows as the first result set. After that you can add the limit and offset to your first result set query.

```
CREATE PROCEDURE get_users_paginate(IN _email VARCHAR(255), IN _per_page INT, IN _page INT)
BEGIN
    SELECT COUNT(*) as total_rows FROM users WHERE email like _email;

    SELECT * FROM users WHERE email like _email LIMIT _per_page OFFSET _page;
END;
```

### When to use this?

[](#when-to-use-this)

If your just doing simple queries you probably wont see any speed increase but if you are doing complex queries with multiple joins and subqueries then you will probably see a significant speed increase.

Installation
------------

[](#installation)

Simply just add the package to your project.

```
composer require zapo/laravel-stored-procedure
```

Then add trait to your model.

```
use JLW\LaravelStoredProcedure\StoredProcedureTrait;

class User extends Model
{
    use StoredProcedureTrait;
}
```

Testing
-------

[](#testing)

```
./vendor/bin/pest
```

License
-------

[](#license)

MIT © [James Waring](https://github.com/jameswaring)

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity14

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/17b1a40d65e3843accbb4c32509f8e28e897bb5b768f1d9f521a140649973d40?d=identicon)[JLW](/maintainers/JLW)

---

Top Contributors

[![james-waring](https://avatars.githubusercontent.com/u/4255174?v=4)](https://github.com/james-waring "james-waring (1 commits)")

### Embed Badge

![Health badge](/badges/jlw-laravel-stored-procedure/health.svg)

```
[![Health](https://phpackages.com/badges/jlw-laravel-stored-procedure/health.svg)](https://phpackages.com/packages/jlw-laravel-stored-procedure)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
