【问题标题】:Redirect with Array in Laravel在 Laravel 中使用数组重定向
【发布时间】:2021-04-05 17:09:14
【问题描述】:

在 Laravel 8 中,如果我想重定向到命名路由,我可以使用:

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

它将始终使用“配置文件已更新!”的值重定向“状态”然后我可以在我的视图中显示:

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

但是我怎样才能使用redirect()->route() 传递一个数组 而不是仅仅传递一个值呢?

【问题讨论】:

  • 你试过-&gt;with('status', [1, 2, 3]);或类似-&gt;with('status', 'Profile updated!')-&gt;with('array', [1, 2, 3]);的东西吗?

标签: laravel


【解决方案1】:

This是如何实现的:

/**
 * 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;
}

这意味着你可以只传递一个数组作为第一个参数,仅此而已。

return redirect()->route( 'success' )->with(['foo' => 'bar']);

【讨论】:

    猜你喜欢
    • 2017-12-24
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多