macsidigital/laravel-eloquent-extended

There is no license information available for the latest version (3.0.0) of this package.

Eloquent extended is a library to add some additional functions to laravel eloquent

3.0.0 2024-06-13 10:40 UTC

This package is auto-updated.

Last update: 2024-12-21 16:31:07 UTC


README

Extended model attributes, Mulit Language attributes and Slugs

Header Image

tests badge version badge downloads badge

Extended Eloquent Models, mainly for JSON and Multi Language Content

Support us

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.

Installation

You can install the package via composer:

composer require macsidigital/laravel-eloquent-extended

Usage

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;

Content

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'

Slugs

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

Route Model Binding

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.

Testing

We have a test suite testing our implementations, to use just run phpunit

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email info@macsi.co.uk instead of using the issue tracker.

Credits

License

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