Extended Eloquent Models, mainly for JSON and Multi Language Content
We invest a lot in creating open source packages, and would be grateful for a sponsor if you make money from your product that uses them.
You can install the package via composer:
composer require macsidigital/laravel-eloquent-extended
To use extended we just need to add the trait and add a protected extendedAttributes variable like so
use Extended\Traits\IsExtended;
use Illuminate\Database\Eloquent\Model;
class TestExtendedModel extends Model
{
use IsExtended;
protected $extendedAttributes = [
'test_field',
];
}
Once set it will act like a normal field
$test = new model;
$test->test_field = 'something';
echo $test->test_field;
To use content is similar with the exception that we can set languages
use Extended\Traits\IsExtended;
use Extended\Traits\HasContent;
use Illuminate\Database\Eloquent\Model;
class TestExtendedModel extends Model
{
use IsExtended, HasContent;
protected $contentAttributes = [
'test_content_field',
];
}
Once set it will act like a normal field
$test = new model;
$test->test_content_field = 'something';
echo $test->test_content_field;
We can set and get different languages like so
$test = new model;
$test->test_content_field = 'something';
$test->setContentLanguage('de');
$test->test_content_field = 'something DE';
$test->setContentLanguage('en');
echo $test->test_content_field; // 'something'
$test->setContentLanguage('de');
echo $test->test_content_field; // 'something DE'
We can use Multiple Language Slugs by adding both HasContent and HasSlug traits and setting the slug fields.
use Extended\Traits\HasSlug;
use Extended\Traits\IsExtended;
use Extended\Traits\HasContent;
use Illuminate\Database\Eloquent\Model;
class TestExtendedModel extends Model
{
use IsExtended, HasContent, HasSlug;
protected $contentAttributes = [
'uri',
];
protected $findSlugField = 'extended->uri';
protected $slugField = 'uri';
}
You can then add the uri like so
$test = new model;
$test->uri = 'something';
echo $test->uri; //esomething
To ensure there are no duplicate slugs you can use the method createSlug like so
$test = new model;
$test->createSlug('Test Something');
echo $test->uri; //test-something
$test = new model;
$test->createSlug('Test Something');
echo $test->uri; //test-something-h58s
We can set and get different languages like so
$test = new model;
$test->uri = 'something';
$test->setLanguage('de');
$test->uri = 'something-de';
$test->setMetaLanguage('en');
echo $test->uri; // 'something'
$test->setMetaLanguage('de');
echo $test->uri; // 'something-de'
We can then retrieve by the slug with the withSlug scoped query method
$test = new model;
$test->createSlug('something');
$model = model::withSlug('something')->first()
echo $model->uri; // 'something'
There is also a reversed function to get all models without the slug
$test = new model;
$test->createSlug('something');
$test = new model;
$test->createSlug('something-1234');
$model = model::withoutSlug('something')->first()
echo $model->uri; // 'something-1234'
We can also use slugs outside of the multi language scope, just set to a normal database field.
use Extended\Traits\HasSlug;
use Extended\Traits\IsExtended;
use Extended\Traits\HasContent;
use Illuminate\Database\Eloquent\Model;
class TestExtendedModel extends Model
{
use HasSlug;
protected $findSlugField = 'uri';
protected $slugField = 'uri';
}
Then all functions will work as previous
You can use {item:slug} in routes to automatically retrieve items by their slug. Just remember to Typehint the model in the controller/Route action.
We have a test suite testing our implementations, to use just run phpunit
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.