Laravel - Controller

laravel

How can our controller method redirect the user to the previous page?

return redirect()->back();

How can our controller method redirect the user to another route?

return redirect()->route('dashboard');

How can we get the user model from the request?

$request->user()

The above code gives us the user model object for the currently authenticated user, so that we can do:

$request->user()->posts()->save($post)

to save a new post. This works because we previously defined the belongsTo and the hasMany relationships.

How can we send a message when we redirect?

return redirect()->route('dashboard')->with(['message' => $message]);

How can we make our models available to our view?

In our controller functions, we have:

public function getDashboard() {
  $posts = Post::all();
  return view('dashboard', ['posts' => $posts]);
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License