| Package Data | |
|---|---|
| Maintainer Username: | binafy |
| Maintainer Contact: | milwad.dev@gmail.com (Milwad Khosravi) |
| Package Create Date: | 2024-03-02 |
| Package Last Update: | 2025-08-18 |
| Home Page: | https://packagist.org/packages/binafy/laravel-stub |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-10-25 15:00:44 |
| Package Statistics | |
|---|---|
| Total Downloads: | 85,766 |
| Monthly Downloads: | 11,102 |
| Daily Downloads: | 238 |
| Total Stars: | 98 |
| Total Watchers: | 3 |
| Total Forks: | 6 |
| Total Open Issues: | 0 |
The Laravel-Stub package enhances the development workflow in Laravel by providing a set of customizable stubs. Stubs are templates used to scaffold code snippets for various components like models, controllers, and migrations. With Laravel-Stub, developers can easily tailor these stubs to match their project's coding standards and conventions. This package aims to streamline the code generation process, fostering consistency and efficiency in Laravel projects. Explore the customization options and boost your development speed with Laravel-Stub.
PHP >= 8.0
Laravel >= 9.0
| Version/Laravel | L9 | L10 | L11 | |-----------------|--------------------|--------------------|--------------------| | 1.0 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
You can install the package with Composer:
composer require binafy/laravel-stub
You don't need to publish anything.
First of all, create a stub file called model.stub:
touch model.stub
Add some code to that, like this:
<?php
namespace {{ NAMESPACE }};
class {{ CLASS }}
{
}
You may use Laravel Stub, you need to use the LaravelStub facade:
use Binafy\LaravelStub\Facades\LaravelStub;
LaravelStub::class;
fromFirst thing, you need to use the from method to give the stub path:
LaravelStub::from(__DIR__ . 'model.stub');
toSo, you need to determine the destination path of the stub file:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App');
nameYou can determine the stub file but also attention don't write the stub extension:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model');
extYou can determine the stub extension:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php');
replaceThe replace method takes two parameters, the first one is the key (variable) and the second one is the value. The value will be replaced with the variable:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replace('NAMESPACE', 'App');
replacesThe replaces method takes an array. If you want to replace multiple variables you can use this method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
]);
moveStubBy default, Laravel Stub creates a copy from your stub file and moves it to the destination path. If you want to move the current stub file, you can use the moveStub method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->moveStub();
After running this code, the model.stub didn't exist.
conditionsThe conditions method allows you to define conditional blocks within your stub files.
You can specify conditions that determine whether certain parts of the stub should be included or excluded based on provided values or closures.
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->conditions([
'hasController' => true,
'hasController' => fn() => false, // or with closure
])
->generate();
NOTE: Ensure that your stub files contain the appropriate conditional placeholders (e.g., {{ if isEnabled }}) to utilize this method effectively.
Your stub code should looks like this:
<?php
namespace {{ NAMESPACE }};
class {{ CLASS }}
{
{{ if hasController }}
public function controller()
{
// Controller logic here
}
{{ endif }}
}
downloadIf you want to download the stub file, you can use the download method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->download(); // Return download response
generateThe important method is generate. To generate the stub file at the end you need to use the generate method to generate stub file:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generate();
NOTE: Don't use the
downloadandgeneratemethods in one chain.
The final file will be like this (new-model.php):
<?php
namespace App;
class Milwad
{
}
generateForceIf you want to generate a stub file and overwrite it if it exists, you can use the generateForce method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateForce();
generateIfIf you want to generate a stub file if given boolean expression evaluates to true, you can use the generateIf method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateIf(true);
generateUnlessIf you want to generate a stub file if given boolean expression evaluates to false, you can use the generateIf method:
LaravelStub::from(__DIR__ . 'model.stub')
->to(__DIR__ . '/App')
->name('new-model')
->ext('php')
->replaces([
'NAMESPACE' => 'App',
'CLASS' => 'Milwad'
])
->generateUnless(true);
Thanks to all the people who contributed. Contributors.
If you discover any security-related issues, please email binafy23@gmail.com instead of using the issue tracker.
The changelog can be found in the CHANGELOG.md file of the GitHub repository. It lists the changes, bug fixes, and improvements made to each version of the Laravel User Monitoring package.
The MIT License (MIT). Please see License File for more information.
If this package is helpful for you, you can buy a coffee for me :) ❤️
0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58