10 months ago by Master Jupiter

Relates to the lesson Build a classified ads site

Why this?

Route::get('/user/area/{area}', 'User\AreaController@store')->name('user.area.store');

Area Controller

public function store(Area $area)
    {
        session()->put('area', $area->slug);
        return redirect()->route('category.index', [$area]);
    }

When we can do this?

Category controller

Route::get('/user/area/{area}', 'Category\CategoryController@index')->name('user.area.store');

public function index(Area $area)
    {
        session()->put('area', $area->slug);
        $categories = Category::get()->toTree();
        return view('categories.index', compact('categories'));
    }```

Is it necessary to use store method in area controller ... put to session and redirect to category controller when it can all be done in category controller?

10 months ago by Grant Savage

It's really just a matter of separating out the logic. Sure, you could technically implement it the way you suggested, but what if you wanted to do the same thing somewhere else? It's a lot easier just calling

return redirect()->route('category.index', [$area]);

than writing

$categories = Category::get()->toTree();
      return view('categories.index', compact('categories'));

multiple times.

Sign in or create an account to reply to this topic.