Skip to content

Instantly share code, notes, and snippets.

@lukemartin
Created June 22, 2012 10:51
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save lukemartin/2972059 to your computer and use it in GitHub Desktop.
Save lukemartin/2972059 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>
@fanibig
Copy link

fanibig commented Nov 4, 2016

plz some one help me how to edit post with multiple categories in edit page. how to get selected categories to select show on edit page. how to retrieve.
result with checkbox

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment