Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

virgiltu's avatar

Blade checkbox and Eloquent

I can do this as a longer version but I am sure there is a short way of doing this and I am missing it.

In my database I have a table Manufacturer and one of the fields is status (boolean) Migration is set to boolean but the database saves it at 0 an 1 . When I try to edit the filed I set the boolean to checked. However that returns ON or OFF ending up in an error.

My Blade checkbox

input type="checkbox" name="status" data-plugin="switchery" data-color="#1bb99a" {{ $manufacturer->status ? 'checked="1"' : 'checked="0"' }} >

My Controller

manufacturer::find($request->id)->update($request->all());

How do i tell elequent that a missing check mark is not 0?

All the help is appreciated.

0 likes
4 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

How do i tell elequent that a missing check mark is not 0?

  • checked is 1
  • not checked is 0

If nothing was passed for the checkbox, then 0.

Convert as needed to laravel request, but it looks something like this in php:

$adopted = (isset($_POST['adopted']) == '1' ? '1' : '0'); 

Where adopted is a checkbox, checked 1 = adopted. not checked 0 = available for adoption.

Do an assessor mutator or whatever you need.

For an edit form:

<input type="checkbox" name="adopted" id="adopted" value="1"<?php echo ($cat->adopted == 1 ? ' checked' : '');

or

<input type="checkbox" name="adopted" id="adopted" value="1" {{  ($cat->adopted == 1 ? ' checked' : '') }}

Don't over think blade, it's just php.

virgiltu's avatar

@JLRDW - Thank you that helped. I am guessing with laravel there is much much shorter way but this did get it to work.

  $manufacturer->drop_ship = (!request()->has('drop_ship') == '1' ? '0' : '1'); 
        $manufacturer->import_only = (!request()->has('import_only') == '1' ? '0' : '1'); 
        $manufacturer->status = (!request()->has('status') == '1' ? '0' : '1'); 
        $manufacturer->name = request('name');
        $manufacturer->markup = request('markup');
        $manufacturer->contact_email = request('contact_email');
        $manufacturer->order_email = request('order_email');
        $manufacturer->telephone = request('telephone');
        $manufacturer->ein = request('ein');
        $manufacturer->comments = request('comments');
        $manufacturer->address = request('address');
        $manufacturer->zipCode = request('zip');
        $manufacturer->state = request('state');
        $manufacturer->country = request('country');

        $manufacturer->save();
      
        return redirect()->back()->with(['status'=>'success', 'message'=>'Updated']);

blade

<input type="checkbox" name="status" data-plugin="switchery" data-color="#1bb99a" {{ $manufacturer->status ? 'checked' : ''}} >

if anybody has a shorter way please let me know. This looks terrible.

Is it possible to just modify one item our of the request and request->all() for the test?

jlrdw's avatar

Read the docs on validation also, your data should be validated or tags stripped via PHP strip tags.

patrickcm's avatar

I agree, it's not quite pretty. First, you are essentially inverting it by saying if drop_ship is 1, then use the string '0', but then use the ! to fix that.

(!request()->has('drop_ship') == '1' ? '0' : '1')

Could simply be

request('drop_ship') == true; or (bool)request('drop_ship')

Please or to participate in this conversation.