Add additional columns (meta boxes) to EloquentModel without add columns to the migration file
Via Composer
$ composer require lao9s/modelmetabox
Use the ModelMetaBox trait on the model you intend to track
use lao9s\ModelMetaBox\Traits\ModelMetaBoxTrait;
class Article extends Model
{
use ModelMetaBoxTrait;
...
}
Publish config file.
php artisan vendor:publish --tag=ModelMetaBoxConfig
Migrate
php artisan migrate
Change config file:
// Override the delete method in the model to delete meta boxes before the model is deleted.
'override_the_delete_method' => true,
Here are some code examples:
// Add/Update meta box to the article
$article->addMetaBox('author', 'Dima Botezatu');
// Support array data. Object or array data will be automatically converted to json.
// If you have another type of data, you can manually convert it into json by adding the third true parameter
$article->addMetaBox('images', ['image1.jpg', 'image2.jpg', 'image3.jpg']);
// Get meta box by specific key. $withValue parameter will return only value not the model object
$article->getMetaBox('author', $withValue = false)
// Also the meta box model have any methods: getValue, getKey or isJson
// Working only if you get metabox object model
$author = $article->getMetaBox('author')
$author->getValue();
$author->getKey();
$author->isJson();
// Get all meta boxes. $withValue parameter will return only values not the models object
$article->getAllMetaBoxes($withValue = false)
// Remove meta box
$article->removeMetaBox('author');
// Remove all meta boxes
$article->removeAllMetaBoxes();
MIT. Please see the license file for more information.