Download the PHP package magslabs/laravel-storedproc without Composer
On this page you can find all versions of the php package magslabs/laravel-storedproc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download magslabs/laravel-storedproc
More information about magslabs/laravel-storedproc
Files in magslabs/laravel-storedproc
Package laravel-storedproc
Short Description A fluent Laravel package for calling stored procedures with support for OUTPUT parameters, pagination, and transactions.
License MIT
Homepage https://github.com/magslabs/laravel-storedproc
Informations about the package laravel-storedproc
Laravel-Storedproc
Laravel-Storedproc is a fluent wrapper for calling stored procedures in Laravel β because not every query belongs in Eloquent.
In many real-world applications, especially enterprise systems, stored procedures are a key part of the backend. Laravel doesnβt natively support them in an elegant way, so most developers are stuck writing raw DB::select() queries over and over.
This package simplifies that.
Features
- Fluent, chainable syntax for stored procedure calls (with shorter method aliases)
- Parameter binding from arrays or Laravel requests
- Optional Laravel-managed transaction support with automatic rollback on failure
- Connection-aware drivers for MySQL and SQL Server (
CALL/EXECresolved per connection) - Automatic existence check before every
execute()/run()(v2 default; configurable) - Opt-in parameter validation against the database schema
- OUTPUT/OUT parameter support for SQL Server and MySQL stored procedures
- Built-in pagination macro for Laravel Collections
- Schema qualification (
dbo.sp_users,database.procedure, orstored_procedure_schema()) - Enhanced logging with dedicated log channel
- Smart result handling (datasets + OUTPUT parameters)
- Typed exceptions for missing procedures, parameter mismatches, and invalid call order
Installation
You can install the package via Composer:
The package will automatically register the StoredProcedureServiceProvider (binding), PaginationServiceProvider (Collection paginate macro), and the StoredProcedure facade alias.
Configuration (optional)
Publish the config file to customize validation and schema defaults:
This creates config/storedproc.php. You can also set these via .env:
| Variable | Default | Description |
|---|---|---|
STORED_PROC_CHECK_EXISTS |
true |
Verify the procedure exists before every execute() / run() |
STORED_PROC_VALIDATE |
false |
Fully validate parameters (count, direction) before execution |
STORED_PROC_SCHEMA |
dbo |
Default schema when the procedure name has no prefix (SQL Server) |
STORED_PROC_CHECK_SYNONYMS |
false |
Treat SQL Server synonyms as callable procedures in existence checks |
Basic Usage
You can use the Facade (when installed in a Laravel app) or the class directly:
Or use the class directly:
Stored procedure without parameters:
With Pagination
With OUTPUT Parameters (SQL Server & MySQL)
SQL Server Example:
MySQL Example:
The result is returned as a Laravel Collection for easy chaining and manipulation, or as an object with result and output properties when OUTPUT parameters are used.
Method Aliases
All original method names remain supported. Shorter aliases are available for the same behavior:
| Original | Alias |
|---|---|
stored_procedure() |
procedure() |
stored_procedure_connection() |
connection() |
stored_procedure_params() |
params() |
stored_procedure_values() |
values() |
stored_procedure_output_params() |
outputs() |
execute() |
run() |
stored_procedure_result() |
result() |
stored_procedure_output_results() |
outputResults() |
Validation & Existence Checks (v2)
Starting in v2.0.0, every execute() / run() automatically verifies the procedure exists on the database before running it. Disable this to restore v1 behavior:
Full parameter validation
Opt in per call with validate(), or globally via .env:
When validation fails, a ParameterMismatchException is thrown with a message listing what the database expects vs. what you provided.
Check existence without executing
Schema qualification
Unqualified names are resolved automatically:
- SQL Server: prefixed with
dbo(orSTORED_PROC_SCHEMA) - MySQL: prefixed with the connection database name
Override explicitly:
On SQL Server, set STORED_PROC_CHECK_SYNONYMS=true if you call procedures via synonyms and want them to pass existence checks.
Parameters & Values
You can pass parameters in multiple formats:
Transaction Support
Enable Laravel-managed transactions like so:
Laravel will automatically commit on success or roll back if the procedure throws an error.
Warning: Use this only if your stored procedure does not manage its own transactions (
BEGIN,COMMIT, etc.).
OUTPUT Parameters (SQL Server & MySQL)
For stored procedures that return OUTPUT/OUT parameters, you can capture them using the stored_procedure_output_params() method:
SQL Server OUTPUT Parameters
MySQL OUT Parameters
OUTPUT/OUT Parameter Usage
Step 1: Include OUTPUT/OUT parameters in stored_procedure_params() with the appropriate keyword:
For SQL Server:
For MySQL:
π‘ Example from your use case:
Step 2: Define SQL types in stored_procedure_output_params():
Step 3: Only provide input values in stored_procedure_values():
Smart Result Handling
The package automatically detects if OUTPUT/OUT parameters are used and returns an object with both result (dataset) and output (parameters):
Pagination Support
The package includes a built-in pagination macro for Laravel Collections, making it easy to paginate stored procedure results:
Pagination Features
- Automatic URL handling: Uses current request URL and query parameters
- Laravel pagination: Full compatibility with Laravel's pagination system
- Customizable: Set custom page name and per-page count
- Blade integration: Works seamlessly with Laravel's pagination views
Example: Full Workflow
This is useful when you want to inject the instance or reuse it across multiple calls. The instance auto-resets after result(); call reset() manually if you need to clear state earlier.
Switching Database Connections
Need to call a stored procedure on a different connection/database?
This uses Laravelβs connection from config/database.php.
Common Gotchas
-
You must call methods in this order:
stored_procedure()/procedure()(required)stored_procedure_connection()/connection()(optional)stored_procedure_schema()(optional)stored_procedure_params()/params()(optional, if your proc has parameters)stored_procedure_values()/values()(required if you set params)stored_procedure_output_params()/outputs()(optional, SQL Server & MySQL)validate()orassertExists()(optional)with_transaction()(optional)execute()/run()(required)stored_procedure_result()/result()(required)
- All input parameters must be bound by position in the
stored_procedure_values()array. - OUTPUT/OUT parameters must be included in
stored_procedure_params()with clean syntax:- SQL Server:
'@result OUTPUT'(OUTPUT keyword required) - MySQL:
'@result'(clean syntax, no OUT keyword needed)
- SQL Server:
- OUTPUT/OUT parameters are supported on both SQL Server and MySQL databases.
- When using OUTPUT/OUT parameters, the result will be an object with
resultandoutputproperties. - Pagination works on the returned Collection, so call
paginate()afterstored_procedure_result(). - After calling
result(), the fluent instance auto-resets so it can be reused safely via dependency injection. - v2 existence checks run before execution by default. Set
STORED_PROC_CHECK_EXISTS=falseif you need v1 behavior. - The StoredProcedureServiceProvider registers the
StoredProcedurebinding; the PaginationServiceProvider registers thepaginate()macro onCollection, so both are available after installation. - The
StoredProcedurefacade alias is registered automatically so you can useStoredProcedure::stored_procedure('name')statically in your app. - Logging: Bound values and output params are logged only at
debuglevel to avoid exposing sensitive data in production logs.
Compatibility
- PHP: 8.0, 8.1, 8.2, 8.3, 8.4 (Laravel 13 requires PHP 8.3+)
- Laravel: 9.x, 10.x, 11.x, 12.x, 13.x (backward compatible; 13.x is the default target)
- Databases:
- MySQL (5.7+, 8.0+)
- SQL Server (2016+, 2019+, 2022+)
- Other databases are not officially supported and may not work as expected
Feature Support by Database
| Feature | MySQL | SQL Server |
|---|---|---|
| Basic stored procedures | β | β |
| Parameters & Values | β | β |
| Transactions | β | β |
| OUTPUT/OUT Parameters | β | β |
| Pagination | β | β |
| Logging | β | β |
| Existence checks (v2) | β | β |
| Parameter validation | β | β |
| Synonym support (check) | β | β |
Upgrading from v1 to v2
v2.0.0 is backward compatible β all v1 method names and chains still work.
Breaking-adjacent changes to be aware of:
- Existence checks are on by default. Every
execute()/run()verifies the procedure exists before calling it. SetSTORED_PROC_CHECK_EXISTS=falseto disable. - Call-order errors now throw
InvalidCallOrderException(still extendsException). - Execution is driver-based.
CALLvsEXECis resolved from the active connection, not a global default.
Publish config when you want to tune validation or schema defaults:
See CHANGELOG.md for the full v2 release notes.
Advanced Usage
Complex Stored Procedure with Multiple OUTPUT/OUT Parameters
SQL Server Example:
MySQL Example:
Pagination with Custom Options
Error Handling and Logging
The package throws typed exceptions you can catch individually:
| Exception | When |
|---|---|
StoredProcedureNotFoundException |
Procedure missing (existence check or DB error) |
ParameterMismatchException |
Parameter count/direction does not match the database |
InvalidCallOrderException |
Fluent methods called in the wrong order |
UnsupportedDriverException |
Connection driver is not MySQL or SQL Server |
All extend StoredProcedureException, which extends PHP's Exception.
Reusing StoredProcedure Instances
Logging Stored Procedure Executions
This package includes built-in logging to help trace and debug stored procedure execution.
Enable a Custom Log File
To log all stored procedure operations into a dedicated log file, add the following channel to your Laravel appβs config/logging.php:
License
MIT License. Β© Mark Angelo Sollano / magslabs
Credits
Created by @masollano β magslabs/laravel-storedproc