routes.php

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

On line 6, you create a variable $cats and assign it the return value of the static 'all' function located within the Cat class. Your comments say that it's to fetch all the categories, but then you never use the variable $cats again. If you're not going to do anything with the returned value, you should just call the method from the class statically while not assigning a value to a new, unused variable. The line can just simply state:

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.