Skip to content

Instantly share code, notes, and snippets.

@coderdiaz
Forked from lukemartin/routes.php
Created February 21, 2016 06:23
Show Gist options
  • Save coderdiaz/43d8a4dd6f96be39f250 to your computer and use it in GitHub Desktop.
Save coderdiaz/43d8a4dd6f96be39f250 to your computer and use it in GitHub Desktop.
Output checkboxes with appropriate 'checked' attributes in Laravel
Route::get('edit', function() {
// fetch our post, and it's associated categories
$post = Post::with('cats')->where('id', '=', $id)->first();
// fetch all of our categories
$cats = Cat::all();
// create our empty array
$post_cats = array();
// loop through each post category, and add the id to our array
foreach ($post->cats as $cat) {
$post_cats[] = $cat->id;
}
return View::make('edit'))->with('post', $post)
->with('cats', $cat)
->with('posts_cats', $posts_cats);
});
<p>Categories</p>
<ul>
@foreach($cats as $cat)
<li>{{ Form::checkbox('cats[]', $cat->id, in_array($cat->id, $post_cats)) }} {{ Form::label('cats_'.$cat->id, $cat->title) }}</li>
@endforeach
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment