├── Documentation
└── en.md
├── README.md
├── art
└── Laravel-Metabox.png
├── composer.json
├── screenshots
├── checkbox.png
├── gallery.png
├── radio.png
├── select.png
├── tabs.png
├── text.png
├── toggle.png
└── uploadImage.png
├── src
├── Database
│ ├── factories
│ │ ├── MetaboxFactory.php
│ │ └── PostFactory.php
│ └── migrations
│ │ └── 2025_03_13_233033_create_metaboxes_table.php
├── Facade
│ └── MetaboxFacade.php
├── Metabox.php
├── Models
│ └── Metabox.php
├── Resources
│ └── Views
│ │ └── components
│ │ ├── CheckboxMetabox.blade.php
│ │ ├── GalleryMetabox.blade.php
│ │ ├── ImageUploadMetabox.blade.php
│ │ ├── RadioMetabox.blade.php
│ │ ├── SelectMetabox.blade.php
│ │ ├── TabMetabox.blade.php
│ │ └── ToggleMetabox.blade.php
├── ServiceProvider
│ └── MetaboxServiceProvider.php
├── Traits
│ └── HasMetaboxes.php
└── View
│ └── Components
│ ├── CheckboxMetabox.php
│ ├── GalleryMetabox.php
│ ├── ImageUploadMetabox.php
│ ├── RadioMetabox.php
│ ├── SelectMetabox.php
│ ├── TabMetabox.php
│ └── ToggleMetabox.php
└── tests
└── Feature
└── MetaboxTest.php
/Documentation/en.md:
--------------------------------------------------------------------------------
1 | # Text Field
2 |
3 | ### Request
4 |
5 | Request Validation field.
6 |
7 | ```php
8 | 'text_field' => ['required', 'string', 'max:255'],
9 | ```
10 |
11 | ### Controller
12 |
13 | To create a `addMetabox` Text field in store and update, do the following.
14 |
15 | ```php
16 | public function store(Request $request, Post $post)
17 | {
18 | $post->update($request->only(['title', 'content']));
19 | $post->addMetabox('text_field', $request->input('text_field'));
20 | return back();
21 | }
22 |
23 | public function update(Request $request, Post $post)
24 | {
25 | $post->update($request->only(['title', 'content']));
26 | $post->addMetabox('text_field', $request->input('text_field'));
27 | return back();
28 | }
29 | ```
30 |
31 | ### Views
32 |
33 | - create.blade.php
34 |
35 | ```html
36 |
48 | ```
49 |
50 | - edit.blade.php
51 |
52 | ```html
53 |
66 | ```
67 |
68 | ### Display Metabox Data
69 |
70 | To display metabox data in your views, use the `getMetabox` method:
71 |
72 | ```php
73 | Text Field: {{ $post->getMetabox('text_field') }}
74 | ```
75 |
76 | ### Screenshot
77 |
78 |
79 |

80 |
81 |
82 |
83 | # File Upload Field
84 |
85 | ### Request
86 |
87 | Request Validation field.
88 |
89 | ```php
90 | 'img_field' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
91 | ```
92 |
93 | ### Controller
94 |
95 | To create a `uploadImageMetabox` File Upload field in store and update, do the following.
96 |
97 | ```php
98 | public function store(Request $request, Post $post)
99 | {
100 | $post->update($request->only(['title', 'content']));
101 | if ($request->hasFile('img_field')) {
102 | $post->uploadImageMetabox('img_field', $request->file('img_field'));
103 | }
104 | return back();
105 | }
106 |
107 | public function update(Request $request, Post $post)
108 | {
109 | $post->update($request->only(['title', 'content']));
110 | if ($request->hasFile('img_field')) {
111 | $post->uploadImageMetabox('img_field', $request->file('img_field'));
112 | }
113 | return back();
114 | }
115 | ```
116 |
117 | ### Views
118 |
119 | - create.blade.php
120 |
121 | ```html
122 |
133 | ```
134 |
135 | - edit.blade.php
136 |
137 | ```html
138 |
150 | ```
151 | ### The Public Disk
152 |
153 | To create the symbolic link, Enter the following command.
154 |
155 | ```bash
156 | php artisan storage:link
157 | ```
158 |
159 | ### Display Metabox Data
160 |
161 | To display metabox data in your views, use the `getMetabox` method:
162 |
163 | ```php
164 | @if ($post->getMetabox('img_field'))
165 |
166 | @else
167 | No image available.
168 | @endif
169 | ```
170 |
171 | ### Screenshot
172 |
173 |
174 |

175 |
176 |
177 | # Select Field
178 |
179 | ### Request :
180 |
181 | Request Validation field.
182 |
183 | ```php
184 | 'select_field' => 'nullable|string',
185 | ```
186 | ### Controller:
187 |
188 | To create a `addMetabox` Select field in store and update, do the following.
189 |
190 | ```php
191 | public function store(Request $request, Post $post)
192 | {
193 | $post->update($request->only(['title', 'content']));
194 |
195 | if ($request->has('select_field')) {
196 | $post->addMetabox('select_field', $request->input('select_field'));
197 | }
198 |
199 | return back();
200 | }
201 |
202 | public function update(Request $request, Post $post)
203 | {
204 | $post->update($request->only(['title', 'content']));
205 | if ($request->has('select_field')) {
206 | $post->addMetabox('select_field', $request->input('select_field'));
207 | }
208 | return back();
209 | }
210 | ```
211 |
212 | ### Views
213 |
214 | - create.blade.php
215 |
216 | ```html
217 |
235 | ```
236 |
237 | - edit.blade.php
238 |
239 | ```html
240 |
260 | ```
261 |
262 | ### Display Metabox Data
263 |
264 | To display metabox data in your views, use the `getMetabox` method:
265 |
266 | ```php
267 | {{ $post->getMetabox('select_field') ?? 'No category selected' }}
268 | ```
269 |
270 | ### Screenshot
271 |
272 |
273 |

274 |
275 |
276 | # Checkbox Field
277 |
278 | ### Request
279 |
280 | Request Validation field.
281 |
282 | ```php
283 | 'checkbox_field' => 'nullable|boolean',
284 | ```
285 | ### Controller
286 |
287 | To create a `addMetabox` Checkbox field in store and update, do the following.
288 |
289 | ```php
290 | public function store(Request $request, Post $post)
291 | {
292 | $post->update($request->only(['title', 'content']));
293 |
294 | $post->addMetabox('checkbox_field', $request->has('checkbox_field') ? '1' : '0');
295 |
296 | return back();
297 | }
298 |
299 | public function update(Request $request, Post $post)
300 | {
301 | $post->update($request->only(['title', 'content']));
302 |
303 | $post->addMetabox('checkbox_field', $request->has('checkbox_field') ? '1' : '0');
304 |
305 | return back();
306 | }
307 | ```
308 |
309 | ### Views
310 |
311 | - create.blade.php
312 |
313 | ```html
314 |
329 | ```
330 |
331 | - edit.blade.php
332 |
333 | ```html
334 |
351 | ```
352 |
353 | ### Display Metabox Data
354 |
355 | To display metabox data in your views, use the `getMetabox` method:
356 |
357 | ```php
358 | @if($post->getMetabox('checkbox_field') === '1')
359 | Enable
360 | @else
361 | Disable
362 | @endif
363 | ```
364 |
365 | ### Screenshot
366 |
367 |
368 |

369 |
370 |
371 | # Toggle Field
372 |
373 | ### Controller
374 |
375 | To create a `addMetabox` Toggle field in store and update, do the following.
376 |
377 | ```php
378 | public function store(Request $request, Post $post)
379 | {
380 | $post->update($request->only(['title', 'content']));
381 |
382 | $post->addMetabox('toggle_field', $request->input('toggle_field'));
383 |
384 | return back();
385 | }
386 |
387 | public function update(Request $request, Post $post)
388 | {
389 | $post->update($request->only(['title', 'content']));
390 |
391 | $post->addMetabox('toggle_field', $request->input('toggle_field'));
392 |
393 | return back();
394 | }
395 | ```
396 |
397 | ### Views
398 |
399 | - create.blade.php
400 |
401 | ```html
402 |
418 | ```
419 |
420 | - edit.blade.php
421 |
422 | ```html
423 |
441 | ```
442 |
443 | ### Display Metabox Data
444 |
445 | To display metabox data in your views, use the `getMetabox` method:
446 |
447 | ```php
448 | @if($post->getMetabox('toggle_field') === '1')
449 | Enable
450 | @else
451 | Disable
452 | @endif
453 | ```
454 |
455 | ### Screenshot
456 |
457 |
458 |

459 |
460 |
461 | # Radio Field
462 |
463 | ### Controller
464 |
465 | To create a `addMetabox` Radio field in store and update, do the following.
466 |
467 | ```php
468 | public function store(Request $request, Post $post)
469 | {
470 | $post->update($request->only(['title', 'content']));
471 |
472 | $post->addMetabox('radio_field', $request->input('radio_field'));
473 |
474 | return back();
475 | }
476 |
477 | public function update(Request $request, Post $post)
478 | {
479 | $post->update($request->only(['title', 'content']));
480 |
481 | $post->addMetabox('radio_field', $request->input('radio_field'));
482 |
483 | return back();
484 | }
485 | ```
486 |
487 | ### Views
488 |
489 | - create.blade.php
490 |
491 | ```html
492 |
511 | ```
512 |
513 | - edit.blade.php
514 |
515 | ```html
516 |
537 | ```
538 |
539 | ### Display Metabox Data
540 |
541 | To display metabox data in your views, use the `getMetabox` method:
542 |
543 | ```php
544 | @php
545 | $statusLabels = [
546 | 'draft' => 'Draft',
547 | 'published' => 'Published',
548 | 'archived' => 'Archived'
549 | ];
550 | $currentStatus = $post->getMetabox('radio_field');
551 | @endphp
552 |
553 | @if($currentStatus)
554 |
555 | {{ $statusLabels[$currentStatus] ?? $currentStatus }}
556 |
557 | @endif
558 | ```
559 |
560 | ### Screenshot
561 |
562 |
563 |

564 |
565 |
566 | # Tabs Field
567 |
568 | ### Views
569 |
570 | To create a `x-TabMetabox` Tabs field in store and update, do the following.
571 |
572 | - create.blade.php and edit.blade.php
573 |
574 | ```html
575 |
602 | ```
603 |
604 | ### Screenshot
605 |
606 |
607 |

608 |
609 |
610 | # Gallery Images Field
611 |
612 | ### Request
613 |
614 | ```php
615 | 'gallery_field.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
616 | ```
617 |
618 | ### Controller
619 |
620 | To create a `addMetabox` Gallery Images field in store and update, do the following.
621 |
622 | ```php
623 | public function store(Request $request, Post $post)
624 | {
625 | $post->update($request->only(['title', 'content']));
626 |
627 | $gallery = json_decode($post->getMetabox('gallery_field'), true) ?? [];
628 |
629 | if ($request->hasFile('gallery_field')) {
630 | foreach ($request->file('gallery_field') as $file) {
631 | $path = $file->store('gallery', 'public');
632 | $gallery[] = $path;
633 | }
634 | }
635 |
636 | if ($request->post_gallery_existing) {
637 | $existing = array_filter($request->post_gallery_existing);
638 | $gallery = array_merge($existing, $gallery);
639 | }
640 |
641 | $post->addMetabox('gallery_field', json_encode(array_unique($gallery)));
642 |
643 | return back();
644 | }
645 |
646 | public function update(Request $request, Post $post)
647 | {
648 | $post->update($request->only(['title', 'content']));
649 |
650 | $gallery = json_decode($post->getMetabox('gallery_field'), true) ?? [];
651 |
652 | if ($request->hasFile('gallery_field')) {
653 | foreach ($request->file('gallery_field') as $file) {
654 | $path = $file->store('gallery', 'public');
655 | $gallery[] = $path;
656 | }
657 | }
658 |
659 | if ($request->post_gallery_existing) {
660 | $existing = array_filter($request->post_gallery_existing);
661 | $gallery = array_merge($existing, $gallery);
662 | }
663 |
664 | $post->addMetabox('gallery_field', json_encode(array_unique($gallery)));
665 |
666 | return back();
667 | }
668 | ```
669 |
670 | ### Views
671 |
672 | `limit` : Limit on the number of images displayed
673 |
674 | - create.blade.php
675 |
676 | ```html
677 |
691 | ```
692 |
693 | - edit.blade.php
694 |
695 | ```html
696 |
712 | ```
713 |
714 | ### Display Metabox Data
715 |
716 | To display metabox data in your views, use the `getMetabox` method:
717 |
718 | ```php
719 | @php
720 | $galleryData = json_decode($row->getMetabox('gallery_field'), true) ?? [];
721 | @endphp
722 |
723 | @foreach($galleryData as $imagePath)
724 | @if(Storage::disk('public')->exists($imagePath))
725 |
726 |
 }})
727 |
728 | @endif
729 | @endforeach
730 | ```
731 |
732 | ### Screenshot
733 |
734 |
735 |

736 |
737 |
738 |
739 |
740 |
741 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |

9 |

10 |

11 |

12 |

13 |

14 |
15 |
16 |
17 |
Documentation
18 |

19 |
20 |
21 | # Laravel Metabox
22 |
23 | Metabox is a highly useful feature for retrieving or displaying WordPress information. We decided to bring this Metabox functionality to Laravel. With Laravel Metabox, you can define features such as custom fields for posts without the need to create a custom field table in the database.
24 |
25 | # Installs
26 |
27 | Install Package:
28 |
29 | ```bash
30 | composer require rayiumir/laravel-metabox
31 | ```
32 |
33 | After Publish Files:
34 |
35 | ```bash
36 | php artisan vendor:publish --provider="Rayiumir\\LaravelMetabox\\ServiceProvider\\MetaboxServiceProvider"
37 | ```
38 |
39 | And Migration Database:
40 |
41 | ```bash
42 | php artisan migrate
43 | ```
44 |
45 | # How to use
46 |
47 | Calling `HasMetaboxes` in Models `Post.php`:
48 |
49 | ```php
50 | use Rayiumir\LaravelMetabox\Traits\HasMetaboxes;
51 |
52 | use HasMetaboxes;
53 | ```
54 |
55 | To delete post metabox data, place the following function in `Post.php`:
56 |
57 | ```php
58 | protected static function boot(): void
59 | {
60 | parent::boot();
61 |
62 | static::deleting(function ($post) {
63 | $post->metaboxes()->delete();
64 | });
65 | }
66 | ```
67 |
68 | # Task Laravel Metabox
69 |
70 | - [X] Text Field
71 | - [X] Upload Field
72 | - [X] Select Field
73 | - [X] Checkbox Field
74 | - [X] Toggle Field
75 | - [X] Radio Button Field
76 | - [X] Tabs Field
77 | - [X] Gallery Image Field
78 |
79 |
80 |
--------------------------------------------------------------------------------
/art/Laravel-Metabox.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/art/Laravel-Metabox.png
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "rayiumir/laravel-metabox",
3 | "description": "Easy creation of MetaBox for Laravel",
4 | "keywords": [
5 | "laravel-metabox",
6 | "laravel metabox",
7 | "easy metabox",
8 | "laravel",
9 | "laravel metabox package"
10 | ],
11 | "minimum-stability": "stable",
12 | "license": "MIT",
13 | "type": "package",
14 | "authors": [
15 | {
16 | "name": "Raymond Baghumian",
17 | "email": "rayiumir@gmail.com",
18 | "homepage": "https://github.com/Rayiumir",
19 | "role": "Developer"
20 | }
21 | ],
22 | "require": {
23 | "php": "^8.0",
24 | "laravel/framework": "^9.0|^10.0|^11.0|^12.0"
25 | },
26 | "require-dev": {
27 | "pestphp/pest-plugin-laravel": "^1.4.0|^2.0.0|^3.1.0",
28 | "orchestra/testbench": "^7.0|^8.0|^9.0|^10.0"
29 | },
30 | "autoload": {
31 | "psr-4": {
32 | "Rayiumir\\LaravelMetabox\\": "src/"
33 | }
34 | },
35 | "autoload-dev": {
36 | "psr-4": {
37 | "Rayiumir\\LaravelMetabox\\Tests\\": "tests/"
38 | }
39 | },
40 | "config": {
41 | "sort-packages": true,
42 | "allow-plugins": {
43 | "pestphp/pest-plugin": true
44 | }
45 | },
46 | "extra": {
47 | "laravel": {
48 | "providers": [
49 | "Rayiumir\\LaravelMetabox\\ServiceProvider\\MetaboxServiceProvider"
50 | ]
51 | }
52 | },
53 | "scripts": {
54 | "test": "vendor/bin/pest",
55 | "test-coverage": "vendor/bin/pest --coverage-html ./coverage"
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/screenshots/checkbox.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/checkbox.png
--------------------------------------------------------------------------------
/screenshots/gallery.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/gallery.png
--------------------------------------------------------------------------------
/screenshots/radio.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/radio.png
--------------------------------------------------------------------------------
/screenshots/select.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/select.png
--------------------------------------------------------------------------------
/screenshots/tabs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/tabs.png
--------------------------------------------------------------------------------
/screenshots/text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/text.png
--------------------------------------------------------------------------------
/screenshots/toggle.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/toggle.png
--------------------------------------------------------------------------------
/screenshots/uploadImage.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LaraPire/laravel-metabox/b37e3a0f19cc2540fac496187d80e93a219fff7e/screenshots/uploadImage.png
--------------------------------------------------------------------------------
/src/Database/factories/MetaboxFactory.php:
--------------------------------------------------------------------------------
1 |
10 | */
11 | class MetaboxFactory extends Factory
12 | {
13 | protected $model = Post::class;
14 | /**
15 | * Define the model's default state.
16 | *
17 | * @return array
18 | */
19 | public function definition(): array
20 | {
21 | return [
22 | 'model_type' => 'App\Models\Post',
23 | 'model_id' => Post::factory(),
24 | 'key' => $this->faker->word,
25 | 'value' => $this->faker->sentence,
26 | ];
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Database/factories/PostFactory.php:
--------------------------------------------------------------------------------
1 |
11 | */
12 | class PostFactory extends Factory
13 | {
14 | protected $model = Post::class;
15 | /**
16 | * Define the model's default state.
17 | *
18 | * @return array
19 | */
20 | public function definition(): array
21 | {
22 | return [
23 | 'title' => $this->faker->sentence,
24 | 'slug' => $this->faker->slug,
25 | 'img' => $this->faker->imageUrl(),
26 | 'content' => $this->faker->paragraph,
27 | 'user_id' => User::factory(),
28 | ];
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Database/migrations/2025_03_13_233033_create_metaboxes_table.php:
--------------------------------------------------------------------------------
1 | id();
16 | $table->string('model_type');
17 | $table->unsignedBigInteger('model_id');
18 | $table->string('key');
19 | $table->text('value')->nullable();
20 | $table->timestamps();
21 | });
22 | }
23 |
24 | /**
25 | * Reverse the migrations.
26 | */
27 | public function down(): void
28 | {
29 | Schema::dropIfExists('metaboxes');
30 | }
31 | };
32 |
--------------------------------------------------------------------------------
/src/Facade/MetaboxFacade.php:
--------------------------------------------------------------------------------
1 | morphTo();
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/CheckboxMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
12 |
13 |
14 |
28 |
29 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/GalleryMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
32 |
33 |
88 |
89 |
138 |
139 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/ImageUploadMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
43 |
44 |
71 |
72 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/RadioMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
3 | @foreach($options as $value => $label)
4 |
14 | @endforeach
15 |
16 |
17 |
49 |
50 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/SelectMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
28 |
29 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/TabMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
65 |
66 |
98 |
99 |
--------------------------------------------------------------------------------
/src/Resources/Views/components/ToggleMetabox.blade.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
13 | {{ $label }}
14 |
15 |
16 |
74 |
85 |
86 |
--------------------------------------------------------------------------------
/src/ServiceProvider/MetaboxServiceProvider.php:
--------------------------------------------------------------------------------
1 | app->bind('Metabox', function() {
18 | return new Metabox();
19 | });
20 | }
21 | /**
22 | * Bootstrap any package services.
23 | *
24 | * @return void
25 | */
26 | public function boot(): void
27 | {
28 | $this->_loadMigrations();
29 | $this->_loadPublished();
30 | }
31 |
32 | private function _loadMigrations(): void
33 | {
34 | $this->loadMigrationsFrom(__DIR__ . '/../Database/migrations');
35 | }
36 |
37 | private function _loadPublished(): void
38 | {
39 | $this->publishes([
40 | __DIR__.'/../Traits' => app_path('Traits/')
41 | ],'LaravelMetaboxTrait');
42 |
43 | $this->publishes([
44 | __DIR__.'/../Models' => app_path('Models/')
45 | ],'LaravelMetaboxModel');
46 |
47 | $this->publishes([
48 | __DIR__.'/../View/Components' => app_path('View/Components')
49 | ], 'LaravelMetaboxViewComponents');
50 |
51 | $this->publishes([
52 | __DIR__.'/../Database/migrations' => database_path('migrations')
53 | ], 'LaravelMetaboxMigration');
54 |
55 | $this->publishes([
56 | __DIR__.'/../Database/factories' => database_path('factories')
57 | ], 'LaravelMetaboxFactory');
58 |
59 | $this->publishes([
60 | __DIR__.'/../Resources/Views/components' => resource_path('Resources/views/components')
61 | ], 'LaravelMetaboxResourceViewComponents');
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/Traits/HasMetaboxes.php:
--------------------------------------------------------------------------------
1 | morphMany(Metabox::class, 'model');
13 | }
14 |
15 | public function addMetabox($key, $value)
16 | {
17 | return $this->metaboxes()->updateOrCreate(
18 | ['key' => $key],
19 | ['value' => $value]
20 | );
21 | }
22 |
23 | public function getMetabox($key)
24 | {
25 | $metabox = $this->metaboxes()->where('key', $key)->first();
26 | return $metabox ? $metabox->value : null;
27 | }
28 |
29 | public function uploadImageMetabox($key, $file)
30 | {
31 | $oldImage = $this->getMetabox($key);
32 | if ($oldImage) {
33 | Storage::disk('public')->delete($oldImage);
34 | }
35 |
36 | $imagePath = $file->store('uploads', 'public');
37 |
38 | $this->addMetabox($key, $imagePath);
39 |
40 | return $imagePath;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/View/Components/CheckboxMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
21 | $this->label = $label;
22 | $this->checked = $checked;
23 | $this->value = $value;
24 | }
25 |
26 | /**
27 | * Get the view / contents that represent the component.
28 | */
29 | public function render(): View|Closure|string
30 | {
31 | return view('components.CheckboxMetabox');
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/View/Components/GalleryMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
21 | $this->images = is_string($images) ? json_decode($images, true) ?? [] : $images;
22 | $this->limit = $limit;
23 | }
24 |
25 | public function imageExists($path)
26 | {
27 | return Storage::disk('public')->exists($path);
28 | }
29 | /**
30 | * Get the view / contents that represent the component.
31 | */
32 | public function render(): View|Closure|string
33 | {
34 | return view('components.GalleryMetabox', [
35 | 'images' => array_filter($this->images, function($path) {
36 | return $this->imageExists($path);
37 | })
38 | ]);
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/View/Components/ImageUploadMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
19 | $this->value = $value;
20 | }
21 |
22 | /**
23 | * Get the view / contents that represent the component.
24 | */
25 | public function render(): View|Closure|string
26 | {
27 | return view('components.ImageUploadMetabox');
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/View/Components/RadioMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
21 | $this->options = $options;
22 | $this->selected = $selected;
23 | $this->orientation = $orientation;
24 | }
25 |
26 | /**
27 | * Get the view / contents that represent the component.
28 | */
29 | public function render(): View|Closure|string
30 | {
31 | return view('components.RadioMetabox');
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/View/Components/SelectMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
20 | $this->options = $options;
21 | $this->selected = $selected;
22 |
23 | }
24 |
25 | /**
26 | * Get the view / contents that represent the component.
27 | */
28 | public function render(): View|Closure|string
29 | {
30 | return view('components.SelectMetabox');
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/View/Components/TabMetabox.php:
--------------------------------------------------------------------------------
1 | tabs = $tabs;
20 | $this->activeTab = $activeTab ?? array_key_first($tabs);
21 | $this->tabPrefix = $tabPrefix;
22 | }
23 |
24 | /**
25 | * Get the view / contents that represent the component.
26 | */
27 | public function render(): View|Closure|string
28 | {
29 | return view('components.TabMetabox');
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/View/Components/ToggleMetabox.php:
--------------------------------------------------------------------------------
1 | name = $name;
23 | $this->label = $label;
24 | $this->checked = $checked;
25 | $this->onValue = $onValue;
26 | $this->offValue = $offValue;
27 | }
28 | /**
29 | * Get the view / contents that represent the component.
30 | */
31 | public function render(): View|Closure|string
32 | {
33 | return view('components.ToggleMetabox');
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/tests/Feature/MetaboxTest.php:
--------------------------------------------------------------------------------
1 | create();
14 | $post->addMetabox('metabox_field', 'Test Value');
15 |
16 | $this->assertDatabaseHas('posts', ['id' => $post->id]);
17 | $this->assertDatabaseHas('metaboxes', [
18 | 'model_type' => Post::class,
19 | 'model_id' => $post->id,
20 | 'key' => 'metabox_field',
21 | 'value' => 'Test Value',
22 | ]);
23 |
24 | $post->delete();
25 |
26 | $this->assertDatabaseMissing('posts', ['id' => $post->id]);
27 | $this->assertDatabaseMissing('metaboxes', [
28 | 'model_type' => Post::class,
29 | 'model_id' => $post->id,
30 | 'key' => 'metabox_field',
31 | 'value' => 'Test Value',
32 | ]);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------