If you have worked with WordPress, WordPress has a feature called custom metabox. The work of metabox in WordPress is that you create fields, you don’t need to create a database table related to its field. So the metabox itself has a special table that stores the received field data in key and value.
Well, we brought this feature of WordPress metabox to Laravel.
Installs
Install Package:
composer require rayiumir/laravel-metabox
After Publish Files:
php artisan vendor:publish --provider="Rayiumir\\LaravelMetabox\\ServiceProvider\\MetaboxServiceProvider"
And Migration Database:
php artisan migrate
How to use
Calling HasMetaboxes
in Models Post.php
:
use Rayiumir\LaravelMetabox\Traits\HasMetaboxes;
use HasMetaboxes;
To delete post metabox data, place the following function in Post.php
:
protected static function boot(): void
{
parent::boot();
static::deleting(function ($post) {
$post->metaboxes()->delete();
});
}
Read the following documentation to work with fields:
Top comments (1)
You are aware a model represents a table, right?
Using a key value table makes no sense when you can add a field to a model.
I just checked custom meta boxes. What I understand from the documentation is it a way to create an admin component. It can be a form where you add metadata, but it could also be the visitor number of the post. from the documentation:
So if you want to build something like that I think you should focus on Laravel dashboard solutions like Nova.