-
-
Save coderdiaz/43d8a4dd6f96be39f250 to your computer and use it in GitHub Desktop.
Output checkboxes with appropriate 'checked' attributes in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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