mercredi 27 juin 2018

Passing data to all views served by a controller in Laravel

I have a controller that has two methods, each serving up a different view:

class NavController extends Controller {
 public function posts()
 {
  $posts = Post::all();
  $tabs = Tag::all();
  return view('posts')->with('posts', => $posts, 'tags' => $tags);
 }
 public function users()
 {
  $users = User::all();
  $tabs = Tag::all();
  return view('users')->with('users', => $users, 'tags' => $tags);
 }
}

I wish to avoid writing the logic for retrieving and passing tags in each and every method and thus I am wondering if there is a way to retrieve the data in one place in the controller and then pass it by default to every view that is served up by this controller.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire