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);
});
Output checkboxes with appropriate 'checked' attributes in Laravel
1 Response
Cat::all();
... unless you planned to do some error checking and/or use $cats, then the variable is entirely superfluous and a waste of CPU cycles and memory both.
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.