Send parameter from one route to another

Asked

Viewed 247 times

2

I want to send a variable from one route to another, to display this variable in the view, use example:

public function create(ExamRequest $request)
{
    Exam::create( $request->all() );
    $message = 'A avaliação "'.$request->input('name').'" foi registrada!';
    return redirect()->route('exams')->withMessage($message);
}

The route exams in my case calls a view, and I try to display the parameter in the same way:

<div class="row text-success text-center">
    {{ isset($message) ? $message : '' }}
</div>

But nothing is ever displayed, as I can send a parameter to another route?

PS: I know with view works, example view('exams')->withMessage($message);, but in case does not change the browser link, and I want that link to stick.

2 answers

2

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

If it is configured in the Routes file an action for the link exams, will work.

<div class="row text-success text-center">
    {!! (Session::has('message')) ? Session::get('message') : '' !!}
</div>

2


There’s nothing wrong with your redirect.

In fact, the method with¹ stores the value temporarily passed in the session, until it is accessed. This is called "Session Flash" in most frameworks.

1 - Both in the normal call of the method and in the magic call of the method with the data is sent to the flash

To access the value, you would have to do so in the view:

 {!! session('message') !!}
  • I don’t like the conventional way.

  • Though I use sometimes.

  • I took the "conventional" because it got a little strange. Actually, Laravel has a thousand ways to use the same function, kkkkkkkkk

  • There’s nothing. There must be less than 10.

  • with([flash' => $message]), with('flash', $message), withFlash($message), session()->flash('flash', $message), session()->flash(['flash' => $message]) ... tired...

  • So there’s five of you...

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.