DEV Community

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

Posted 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:

Text Field

Image Upload Field

GitHub

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (1)

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay