【问题标题】:Laravel 6: redirect with variables AND message - not workingLaravel 6:使用变量和消息重定向 - 不起作用
【发布时间】:2020-05-21 06:53:36
【问题描述】:


我正在尝试使用我的主控制器中的函数重定向变量和消息。
消息码没问题,一般用->with('abo', 'messagetext')显示。
问题是不是两种类型都被重定向,只有变量(没有消息):(


目标是重定向以下变量:suchString、userId、suche
public function subscribe(Request $request)
    {
        $suchString = $request->input('suchString');
        $userId = $request->input('userId');
        Subscribe::create([
            'abo_string' => $suchString,
            'userid' => $userId
        ]);
            $suche = Document::where( 'title', 'LIKE', '%' . $suchString . '%' )
                                ->orWhere( 'category', 'LIKE', '%' . $suchString . '%' )
                                ->orWhere( 'description', 'LIKE', '%' . $suchString . '%' )
                                ->get();
        $users = User::all();
        return view('pages.search')->with('abo', 'messagetext');
    } 

【问题讨论】:

    标签: php laravel variables redirect multiple-variable-return


    【解决方案1】:

    您应该尝试compact() 方法来重定向您的变量。

     session()->flash( 'abo', 'Your message' );
     return view('pages.search', compact('suchString', 'userId', 'suche' ));
    

    【讨论】:

    • 感谢您的快速回复 :) 是否也可以添加消息?我正在尝试返回两个
    • @Dodo43 如果你想传递消息然后在compact()方法中添加一个新变量
    • @Dodo43 从逻辑上讲,如果您需要传递两个以上的变量,后一种方法可能会变得相当笨拙。您可以使用 PHP 的 compact() 函数节省一些输入:
    • 我也尝试过你的方法,但是在调用视图之后没有显示示例消息:/但也没有错误消息......
    • @Dodo43 您如何将“消息文本”检索到视图中?还有其他变量是否可以访问?喜欢dd($suchString)。感谢您尝试我的解决方案。
    【解决方案2】:

    您返回的是 view() 而不是 redirect()

    您也可以随意链接with()

        return redirect()->route('pages.search')
            ->with('abo', 'messagetext')
            ->with('suche', $suche)
            ->with('userId', $userId)
            ->with('suchString', $suchString);
    

    很可能是你想要的。

    这些都将在您的下一个页面视图的会话中。

    【讨论】:

    • 以某种方式显示消息,但不显示变量。可能是它的输入,我不知道:/
    猜你喜欢
    • 2014-04-18
    • 1970-01-01
    • 2018-01-30
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多