Created
June 22, 2012 10:51
-
-
Save lukemartin/2972059 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
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