skip to Main Content

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


  1. return redirect()->route('Communities.Index')
    
    Login or Signup to reply.
  2. 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.');

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search