Level 53
Feb 1, 2015
4
Level 5
Saving a checkbox
In my page I have 6 checkboxes and I would like the user to be able to select 1 or more checkboxes.
I have the checkboxes showing the correct values but I'm having a problem saving them. It's probably something small.
My BannersController.php
public function create()
{
$banners = Banner::with('frame')->get();
$frame_options = Frame::lists('title', 'id');
$menu_options = Menu::lists('title', 'id');
return View::make('banners::banners.create', compact('banners', 'frame_options', 'menu_options'))->with($frame_options, $menu_options);
//return View::make('banners::banners.create');
}
/**
* Store a newly created resource in storage.
* POST /bannerss
*
* @return Response
*/
public function store()
{
$input = Input::all();
$validation = Validator::make($input, Banner::$rules);
if($validation->fails()){
return Redirect::route('banners::banners.index')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors');
}
if($validation->passes()){
$banners = new Banner();
$frameId = (array) array_get($input, 'frame_id');
$menuId = (array) array_get($input, 'menu_id');
$banners->fill($input)->save();
$banners->frame()->sync($frameId);
$banners->menu()->sync($menuId);
$banners = Banner::all();
return View::make('banners::banners.index', compact('banners'));
}
}
My Banner.php
<?php
class Banner extends \Eloquent {
protected $fillable = array('title');
protected $guarded = array('id');
protected $table = 'banners';
public static $rules = array(
);
public function frame(){
return $this->belongsToMany('Frame', 'banners_frame', 'banner_id', 'frame_id');
}
public function menu(){
return $this->belongsToMany('Menu', 'banners_menu', 'banner_id', 'menu_id');
}
}
My Menu.php
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Menu extends \Eloquent implements UserInterface, RemindableInterface{
use UserTrait, RemindableTrait;
protected $fillable = array('title', 'parent');
protected $guarded = array('id');
protected $table = 'menus';
public static $rules = array(
'title' => 'required'
);
public function parents(){
return $this->hasMany('Parents');
}
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');
}
}
My create.blade.php
@extends('templates::admin')
@section('content')
<div class="row">
<div class="col-lg-12">
<p>This is the create banner</p>
{{ Form::open(array('route' => 'admin.banners.store', 'class' => 'add-form')) }}
<div class="form">
<div>
{{ Form::label('title', 'Title') }}
</div>
<div>
{{ Form::text('title','', array('id' => 'title', "class" => "form-control")) }}
</div>
<p>
Multiple select
</p>
<div>
{{ Form::select('frame_id[]', $frame_options, '', ['multiple']) }}
</div>
<p>
Checkbox
</p>
<div>
<ul>
@foreach($menu_options as $menu_option)
<li>
{{ $menu_option }}
<input tabindex="1" type="checkbox" name="$menu_option[]" id="test" value="{{ $menu_option }}">
</li>
@endforeach
</ul>
</div>
<div>
{{ Form::submit('Submit', array("class" => "btn btn-default submit", "role" => "button")) }}
</div>
</div>
{{ Form::close() }}
</div>
</div>
@stop
Level 5
Thanks to both bestmomo and JarekTkaczyk I used a combination of both of their solutions
@foreach($menu_options as $option_id => $menu_option)
<li>
{{ $menu_option }}
<input tabindex="1" type="checkbox" name="menu_id[]" id="test" value="{{ $option_id }}">
</li>
@endforeach
Please or to participate in this conversation.