Saltar al contenido
← Volver a proyectos

Laravel Benefit

A Laravel package providing a translatable Benefit model, powered by spatie/laravel-translatable

Laravel Benefit

#Laravel Benefit

Buy Me A Coffee

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads License

A simple Laravel package providing a translatable Benefit Eloquent model, powered by spatie/laravel-translatable.

#Features

  • Benefit model — A generic benefit entity with translatable name and description, plus a unique slug
  • Translatable Contentname and description are translatable via spatie/laravel-translatable, with automatic fallback to the app's fallback locale
  • Configurable Table Name — Override the benefits table name via config
  • Configurable Locales — Mirrors app.available_locales (or the app locale) to describe supported translation locales

#Requirements

  • PHP 8.2+
  • Laravel 12.x or 13.x

#Installation

You can install the package via composer:

composer require jeffersongoncalves/laravel-benefit

Publish and run the migrations:

php artisan vendor:publish --tag="benefit-migrations"
php artisan migrate

Publish the config file (optional):

php artisan vendor:publish --tag="benefit-config"

#Configuration

The config file (config/benefit.php) covers:

#Table Name

'table_names' => [
    'benefits' => 'benefits',
],

#Locales

'locales' => config('app.available_locales')
    ? array_keys(config('app.available_locales'))
    : [config('app.locale', 'en')],

Reads from app.available_locales (an array keyed by locale code, e.g. ['en' => 'English', 'pt_BR' => 'Português']) when present, otherwise falls back to the app's default locale. The package also configures spatie/laravel-translatable's fallback behavior on boot, so a missing translation for the current locale falls back to app.fallback_locale (or any available locale if that is also missing).

#Usage

use JeffersonGoncalves\Benefit\Models\Benefit;

$benefit = Benefit::create([
    'name' => ['en' => 'Free Shipping', 'pt_BR' => 'Frete Grátis'],
    'description' => ['en' => 'No shipping costs on any order.', 'pt_BR' => 'Sem custo de frete em qualquer pedido.'],
    'slug' => 'free-shipping',
]);

$benefit->name; // resolved for the current app locale, with fallback
$benefit->getTranslation('name', 'pt_BR'); // 'Frete Grátis'
$benefit->setTranslation('description', 'es', 'Sin costos de envío.');
$benefit->save();

#Translations

Because the model uses Spatie\Translatable\HasTranslations, the full spatie/laravel-translatable API is available:

$benefit->getTranslation('name', 'pt_BR');
$benefit->setTranslation('name', 'pt_BR', 'Frete Grátis');
$benefit->getTranslations('name'); // ['en' => '...', 'pt_BR' => '...']
$benefit->translate('name', 'pt_BR');

#Route model binding

The model uses slug as its route key:

Route::get('/benefits/{benefit}', fn (Benefit $benefit) => $benefit);
// resolves via the `slug` column

#Testing

composer test

#Changelog

Please see CHANGELOG for more information on what has changed recently.

#Contributing

Please see CONTRIBUTING for details.

#Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

#Credits

#License

The MIT License (MIT). Please see License File for more information.

Nueva versión disponible.