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

Shiva's avatar

Getting a checkbox checked

I've got a couple of checkboxes and I'm not sure how to go about getting it selected if the type is in the database

edit.blade.php

<div class="layout_type">
                <label>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" value="home" name="type"> Home
                        </label>
                    </div>
                </label>

                <label>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" value="about" name="type"> About
                        </label>
                    </div>
                </label>

                <label>
                    <div class="checkbox">
                        <label>
                            <input type="checkbox" value="contact" name="type"> Contact
                        </label>
                    </div>
                </label>
            </div>

MenusController

 public function edit($id)
    {
        $menus = Menu::find($id);
        $menu_id = Menu::lists('title', 'id');
        $selected_type = Menu::lists('title', 'id');
        if(is_null($menus)){
            return Redirect::route('admin.menus.edit');
        }

        return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'))->with($menu_id, $selected_type);
    }
0 likes
20 replies
bobbybouwmann's avatar

You can do something like this if the $selected_type holds the selected values

<div class="checkbox">
    <label>
        <input type="checkbox" value="contact" name="type" @if (in_array($item->id, $selected_type)) checked="checked" @endif> Contact
    </label>
</div>

Or if you find this more readable

<div class="checkbox">
    <label>
        <input type="checkbox" value="contact" name="type" {{ (in_array($item->id, $selected_type)) ? 'checked="checked" ' : '' }}> Contact
    </label>
</div>

Note: you use a double label around your checkbox, you only need one ;)

2 likes
bashy's avatar

You can use a form::model as well to pre populate fields.

2 likes
bobbybouwmann's avatar

@bashy That doesn't work with multiple checkboxes when you have setup a many to many relation right?

Shiva's avatar

Here is the checkbox in my edit.blade.php

<div class="checkbox">
    <label>
        <input type="checkbox" value="contact" name="type" @if (in_array($menus->id, $selected_type)) checked="checked" @endif> Contact
    </label>
</div>

and here is my menus controller

public function edit($id)
    {
        $menus = Menu::find($id);
        $menu_id = Menu::lists('title', 'id');
        $selected_type = Menu::lists('title', 'id');
        if(is_null($menus)){
            return Redirect::route('admin.menus.edit');
        }

        return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'))->with($menu_id, $selected_type);
    }
bobbybouwmann's avatar

First of all you can refactor this

 return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'))->with($menu_id, $selected_type);

To this

 return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'));

You need to check if you have the correct data in front of you! What does this return for you

public function edit($id)
{
    $menus = Menu::find($id);
    $menu_id = Menu::lists('title', 'id');
    $selected_type = Menu::lists('title', 'id');

    dd($menus, $selected_type);

    if (is_null($menus)) {
        return Redirect::route('admin.menus.edit');
    }

    return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'));
}

I would recommend you to update the $menus variable to $menu since it's just one object and the s kinda makes it multiple ;)

Shiva's avatar

@blackbird - When I add dd($menus, $selected_type); I get

object(Menu)[338]
  protected 'fillable' => 
    array (size=4)
      0 => string 'title' (length=5)
      1 => string 'type' (length=4)
      2 => string 'menu_id' (length=7)
      3 => string 'image' (length=5)
  protected 'guarded' => 
    array (size=1)
      0 => string 'id' (length=2)
  protected 'table' => string 'menus' (length=5)
  protected 'connection' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array (size=7)
      'id' => int 36
      'title' => string 'iop' (length=3)
      'type' => string 'home' (length=4)
      'image' => string '[]' (length=2)
      'menu_id' => int 0
      'created_at' => string '2015-03-30 17:08:37' (length=19)
      'updated_at' => string '2015-03-30 17:08:37' (length=19)
  protected 'original' => 
    array (size=7)
      'id' => int 36
      'title' => string 'iop' (length=3)
      'type' => string 'home' (length=4)
      'image' => string '[]' (length=2)
      'menu_id' => int 0
      'created_at' => string '2015-03-30 17:08:37' (length=19)
      'updated_at' => string '2015-03-30 17:08:37' (length=19)
  protected 'relations' => 
    array (size=0)
      empty
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'observables' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  protected 'morphClass' => null
  public 'exists' => boolean true

array (size=4)
  30 => string 'Home' (length=4)
  31 => string 'About Us' (length=8)
  32 => string 'Contact Us' (length=10)
  36 => string 'iop' (length=3)

bobbybouwmann's avatar

This should work

<div class="checkbox">
    <label>
        <input type="checkbox" value="contact" name="type" @if (in_array($menus->id, array_keys($selected_type))) checked="checked" @endif> Contact
    </label>
</div>
Shiva's avatar

@blackbird - if I add another checkbox then that one is also selected instead of staying unselected

Shiva's avatar

I tried to do this in my MenuController

public function edit($id)
    {
        $menus = Menu::find($id);
        $menu_id = Menu::lists('title', 'id');
        $selected_type = $menus->type; //Added

        if(is_null($menus)){
            return Redirect::route('admin.menus.edit');
        }

        return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type'))->with($menu_id, $selected_type);
    }

and this in my edit.blade.php


<div class="checkbox">
    <label>
        <input type="checkbox" value="home" name="type[]" {{ (in_array($menus->id, $selected_type)) ? 'checked="checked" ' : '' }}> Home
    </label>
</div>

and this is the error i get

in_array() expects parameter 2 to be array, string given (View: C:\wamp\www\test\app\modules\menus\views\edit.blade.php) 
Shiva's avatar

@rodrigo.pedra - could you have a look at my code and see where I went wrong.

My MenuController.php

 public function edit($id)
    {
        $menus = Menu::find($id);
        $menu_id = Menu::lists('title', 'id');
        $selected_type = $menus->type; 

        $menuType = Menu::with('type')->findOrFail( $id ); // Added
        $types = Menu::all(); // Added

        if(is_null($menus)){
            return Redirect::route('admin.menus.edit');
        }

        return View::make('menus::edit', compact('menus', 'menu_id', 'selected_type', 'types', 'menuType'))->with($menu_id, $selected_type, $types, $menuType);
    }

My edit.blade.php

 <ul>
        @forelse($types as $type)
        <li>
            <label>
                <input type="checkbox" name="type[]"
                value="{{ $type->id }}" {{ $menuType->type($type) ? 'checked' : '' }}>
                {{ $type->name }}
            </label>
        </li>
        @empty
        <li>no types</li>
    @endforelse
</ul> 

I'm getting this error

Call to undefined method Illuminate\Database\Query\Builder::type() 
rodrigo.pedra's avatar

Reading it carefully, it is not clear to me what you are trying to do. From the previous posts I see that type is a string attribute of Menu. So, it seems the user can only choose only one type for a menu item.

  • Is the list of types a fixed one?
  • Should a Menu have one or more types?
  • Provide the models code and how tables are organized

Please clarify what you want to do, so we can better help you.

From your opening post, I would go with a <select> or a collection of <input type="radio">. So assuming you want a user to choose only one type from a fixed list, you can go this way:

public function edit($id)
{
    $menu = Menu::find($id);

    return View::make('menus.edit', compact('menu'));
}

and your view:

<div class="layout_type">
    <div class="checkbox">
        <label>
            <input type="radio" value="home" name="type" {{ $menu->type === 'home' ? 'selected' : '' }}> Home
        </label>
    </div>

    <div class="checkbox">
        <label>
            <input type="radio" value="about" name="type" {{ $menu->type === 'about' ? 'selected' : '' }}> About
        </label>
    </div>

    <div class="checkbox">
        <label>
            <input type="radio" value="contact" name="type" {{ $menu->type === 'contact' ? 'selected' : '' }}> Contact
        </label>
    </div>
</div>
Shiva's avatar

@rodrigo.pedra - I've tried the code above but the correct type isn't selected.

  1. When you say fixed what do you mean?
  2. No a menu must only have one type
  3. Here is my Menu model
class Menu extends \Eloquent {
    protected $fillable = array('title', 'type', 'menu_id', 'image');
    protected $guarded = array('id');

    protected $table = 'menus';

    public static $rules = array(

        );


    public function menusP(){
        return $this->hasMany('Menu', 'menu_id');
    }


    public function content(){
        return $this->belongsToMany('Content', 'content_menu', 'menu_id', 'content_id');
    }

    public function banner(){
        return $this->belongsToMany('Banner', 'banner_menu', 'menu_id', 'banner_id');
    }

    public function seo(){
        return $this->belongsToMany('Seo', 'menu_seo', 'menu_id', 'seo_id');
    }
}
rodrigo.pedra's avatar
Level 56

My mistake, change selected for checked, I also used the old(...) helper so if the form validation fails it gets back to the selected value before submission

<div class="layout_type">
    <div class="checkbox">
        <label>
            <input type="radio" value="home" name="type" {{ old('type', $menu->type) === 'home' ? 'checked' : '' }}> Home
        </label>
    </div>

    <div class="checkbox">
        <label>
            <input type="radio" value="about" name="type" {{ old('type', $menu->type) === 'about' ? 'checked' : '' }}> About
        </label>
    </div>

    <div class="checkbox">
        <label>
            <input type="radio" value="contact" name="type" {{ old('type', $menu->type) === 'contact' ? 'checked' : '' }}> Contact
        </label>
    </div>
</div>

Tested this time, works.

By fixed list of types I mean a pre-determined list of types that are not subject to change a lot. But I think your model answered my question.

final53's avatar

FOR GOOGLE:

Laravel make a checkbox checked by default

I found a very nice way to do this with blade, without any extra code in controllers. The checkbox should be checked at the first visit of the create page and after a POST/request with a failed validation keep stat that was selected by a user.

Normally I would do something like count($_POST) > 0 but $_POST is not available in blade. So I got the Idea to take the error array.

<div class="checkbox">
    <label>
        <input id="status" name="status" type="checkbox" value="1" 
        {{ $errors->all() ? (old('status') ? 'checked' : '') : 'checked' }}> Active
    </label>
</div>
1 like

Please or to participate in this conversation.