DEV Community

Cover image for Laravel Metabox : Easy creation of custom fields for Laravel
Raymond Baghumian
Raymond Baghumian

Posted on • Edited on

Laravel Metabox : Easy creation of custom fields for Laravel

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 
Enter fullscreen mode Exit fullscreen mode

After Publish Files:

php artisan vendor:publish --provider="Rayiumir\\LaravelMetabox\\ServiceProvider\\MetaboxServiceProvider" 
Enter fullscreen mode Exit fullscreen mode

And Migration Database:

php artisan migrate 
Enter fullscreen mode Exit fullscreen mode

How to use

Calling HasMetaboxes in Models Post.php:

use Rayiumir\LaravelMetabox\Traits\HasMetaboxes; use HasMetaboxes; 
Enter fullscreen mode Exit fullscreen mode

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(); }); } 
Enter fullscreen mode Exit fullscreen mode

Read the following documentation to work with fields:

Documentation

GitHub

Top comments (4)

Collapse
 
xwero profile image
david duymelinck

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:

The content of custom meta boxes are usually HTML form elements where the user enters data related to a Plugin’s purpose, but the content can be practically any HTML you desire.

So if you want to build something like that I think you should focus on Laravel dashboard solutions like Nova.

Collapse
 
rayiumir profile image
Raymond Baghumian • Edited

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, and img.

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!

Collapse
 
xwero profile image
david duymelinck

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?

Thread Thread
 
rayiumir profile image
Raymond Baghumian

This package executes a one query to retrieve all metabox fields, not ten separate queries.

Image description

The metabox data is stored in a structured table and retrieved through an optimized morphTo relationship. This implementation ensures that:

  • All related metabox fields are loaded in a single query using efficient eager loading
  • No N+1 query issues occur, even when accessing multiple metabox records
  • The database structure maintains proper indexing for performance

For example, when fetching a post with +10 metabox fields, the package executes:

  • 1 query to get the post
  • 1 query to get all related metabox data (not 10 separate queries)

This is achieved through Laravel's morphTo relationship with appropriate eager loading.

Thank you for your feedback!