@@ -254,3 +254,92 @@ To display metabox data in your views, use the `getMetabox` method:
254
254
{{ $post->getMetabox('select_field') ?? 'No category selected' }}
255
255
```
256
256
257
+ # Checkbox Field
258
+
259
+ ### Request
260
+
261
+ Request Validation field.
262
+
263
+ ``` php
264
+ 'checkbox_field' => 'nullable|boolean',
265
+ ```
266
+ ### Controller:
267
+
268
+ To create a ` addMetabox ` Checkbox field in store and update, do the following.
269
+
270
+ ``` php
271
+ public function store(Request $request, Post $post)
272
+ {
273
+ $post->update($request->only(['title', 'content']));
274
+
275
+ $post->addMetabox('checkbox_field', $request->has('checkbox_field') ? '1' : '0');
276
+
277
+ return back();
278
+ }
279
+
280
+ public function update(Request $request, Post $post)
281
+ {
282
+ $post->update($request->only(['title', 'content']));
283
+
284
+ $post->addMetabox('checkbox_field', $request->has('checkbox_field') ? '1' : '0');
285
+
286
+ return back();
287
+ }
288
+ ```
289
+
290
+ ### Views
291
+
292
+ - create.blade.php
293
+
294
+ ``` html
295
+ <form action =" {{ route('posts.store') }}" method =" POST" >
296
+ @csrf
297
+ <div >
298
+ <label for =" title" >Title</label >
299
+ <input type =" text" name =" title" id =" title" >
300
+ </div >
301
+ <div >
302
+ <x-CheckboxMetabox
303
+ name =" checkbox_field"
304
+ label =" Enable / Disable"
305
+ value =" 1"
306
+ />
307
+ </div >
308
+ <button type =" submit" >Save</button >
309
+ </form >
310
+ ```
311
+
312
+ - edit.blade.php
313
+
314
+ ``` html
315
+ <form action =" {{ route('posts.update', $post->id) }}" method =" POST" >
316
+ @csrf
317
+ @method('PUT')
318
+ <div >
319
+ <label for =" title" >Title</label >
320
+ <input type =" text" name =" title" value =" {{ $post->title }}" >
321
+ </div >
322
+ <div >
323
+ <x-CheckboxMetabox
324
+ name =" checkbox_field"
325
+ label =" Enable / Disable"
326
+ :checked =" $post->getMetabox('checkbox_field') === '1'"
327
+ value =" 1"
328
+ />
329
+ </div >
330
+ <button type =" submit" >Save</button >
331
+ </form >
332
+ ```
333
+
334
+ ### Display Metabox Data
335
+
336
+ To display metabox data in your views, use the ` getMetabox ` method:
337
+
338
+ ``` php
339
+ @if($post->getMetabox('checkbox_field') === '1')
340
+ <span class =" badge" >Enable</span >
341
+ @else
342
+ <span class =" badge" >Disable</span >
343
+ @endif
344
+ ```
345
+
0 commit comments