A collection of traits, macros and other helpers to keep your Laravel app feeling fresh.
composer require yabhq/laravel-mint
The package ships with some useful stub customizations for common controller and test case starter code.
php artisan vendor:publish --tag="stubs"
Save a model "quietly" without broadcasting any events or firing off any observers.
use Yab\Mint\Traits\SavesQuietly;
class Example extends Model
{
use SavesQuietly;
}
$example->saveQuietly();
Allow for models to be archived or unarchived based on an "archived_at" field on the database table. A global scope automatically excludes archived records when querying your model.
use Yab\Mint\Traits\Archivable;
class Example extends Model
{
use Archivable;
}
$example->archive();
$example->unarchive();
Example::query(); // Will exclude archived items...
Example::withArchived(); // With archived items included...
Models which are marked as "immutable" will throw an ImmutableDataException if updated or deleted (but not created).
use Yab\Mint\Traits\Immutable;
class Example extends Model
{
use Immutable;
}
// No problem
$example = Example::create([
'field' => 'value'
]);
// Throws an exception...
$example->update([
'field' => 'updated'
]);
You can also customize the conditions under which a model is immutable by overriding the isImmutable() function on your model:
public function isImmutable()
{
return $this->status === 'closed';
}
When writing tests using the factory helper, sometimes it is desirable not to trigger any observer logic.
This package includes a built-in macro which helps with this:
/** @test */
public function factory_events_can_be_disabled_on_demand()
{
$example = factory(Example::class)->withoutEvents()->create();
}
A custom cast for storing monetary values as cents in the database while fetching them as decimal values.
use Yab\Mint\Casts\Money;
class Example extends Model
{
protected $casts = [
'price' => Money::class,
];
}
Create slugs that are unique and never collide with each other
use Yab\Mint\Trails\Slugify;
class Example extends Model
{
use Slugify
}
By default the Slugify trait uses the name
property on your model. You can change this
by overriding the getSlugKeyName
method.
use Yab\Mint\Trails\Slugify;
class Example extends Model
{
use Slugify;
public static function getSlugKeyName(): string
{
return 'title';
}
}