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 (4)
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.
Yes, that's right.
This trait called
HasMetaboxes
can be applied to any model that needs metabox, essentially adding a set of methods and capabilities to the class.Imagine you've already created your primary database table (posts), which has fields like
title
,slug
,content
, andimg
.Now, if you later need to add a new field, you'd typically have to roll back or refresh the database, which is time-consuming and increases development time, making the process more complicated.
This is where Laravel Metabox comes in, allowing you to define custom fields in Laravel without modifying the main database table. Instead, the received data is stored in the Laravel Metabox database under the metaboxes table.
This database contains two key tables: key and value. The key table stores the field name, while the value table stores the field's data.
If a post is deleted, the related metabox data is also deleted based on the model's ID just like how WordPress metabox work. We've brought this functionality to Laravel.
Apologies for the delayed response.
Thank you for your feedback!
The reason why I mentioned that the model is a table representation, is that in the case a field needs to be added or removed it is expected that it comes with a migration.
The problem with a key value table in a database are performance issues. Because it is not possible to add indexes.
I haven't checked how the metaboxes are queried, but when there are ten or more fields are that ten queries, or one query?
This package executes a one query to retrieve all metabox fields, not ten separate queries.
The metabox data is stored in a structured table and retrieved through an optimized morphTo relationship. This implementation ensures that:
For example, when fetching a post with +10 metabox fields, the package executes:
This is achieved through Laravel's morphTo relationship with appropriate eager loading.
Thank you for your feedback!