GTS Joe
GTS Joe

Reputation: 4182

Redirect with Array in Laravel

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?

Upvotes: 1

Views: 751

Answers (1)

Luis Montoya
Luis Montoya

Reputation: 3207

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']);

Upvotes: 1

Related Questions