1

In Laravel 8, if I want to redirect to named route I can use:

return redirect()->route( 'success' )->with('status', 'Profile updated!');

It will always redirect "status" with the value of "Profile updated!" which I can then display in my view with:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

But how can I pass an array using redirect()->route() instead of just a single value?

1
  • Have you tried ->with('status', [1, 2, 3]); or something like ->with('status', 'Profile updated!')->with('array', [1, 2, 3]);? Commented Dec 29, 2020 at 1:18

1 Answer 1

1

This is how it is implemented:

/**
 * Flash a piece of data to the session.
 *
 * @param  string|array  $key
 * @param  mixed  $value
 * @return $this
 */
public function with($key, $value = null)
{
    $key = is_array($key) ? $key : [$key => $value];

    foreach ($key as $k => $v) {
        $this->session->flash($k, $v);
    }

    return $this;
}

It means you can just pass an array as the first argument, that's all.

return redirect()->route( 'success' )->with(['foo' => 'bar']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.