I am trying to get a Laravel controller to return to a route after a create function is complete.
I have the following code:
public function store(Request $request)
{
Community::create($request->validated() + ['user_id' =>auth()->id()]);
Return to route('communities.index');
}
It is returning the following error:
syntax error, unexpected identifier "Route", expecting ";"
I know I am overlooking something simple, please help.
2
Answers
For redirection:
return redirect()->route('Communities.Index');
For redirection to previous route:
return redirect()->back();
For redirection with payload:
return redirect()->route('Communities.Index')->with('success', 'Item added to cart successfully!');
return redirect()->back()->with('error', 'This is an error message.');