【问题标题】:Trying to pass a variable to a view, but nothing is shown in the view - laravel试图将变量传递给视图,但视图中没有显示任何内容 - laravel
【发布时间】:2016-07-05 12:57:05
【问题描述】:

试图将变量传递给视图,但视图中没有显示任何内容。

控制器代码:

public function postCreatePost(Request $request, $wordID)
{
    $postsInWord = Post::where('words_id', $wordID)->get();
    return redirect()->back()->with('postsInWord', $postsInWord);

查看代码:

@if (isset($postsInWord))
    @foreach ($postsInWord as $postsInWord)
    <p>{{$postsInWord->body}}</p>
    @endforeach
@endif

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您需要更改return 语句..

    return redirect()->back()->with('data', ['postsInWord']);
    

    试试这个..

    【讨论】:

    • 请尝试在控制器和刀片中简单地打印数组..调试会更好。并且请检查是否发生任何错误或只是显示空白页?
    【解决方案2】:

    您不能使用重定向发送变量。

    虽然你可以通过一些数据来传递会话,比如跟随。

    return redirect()->back()->with('data', 'some kind of data');
    

    然后您可以使用以下命令从刀片模板中获取数据:

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

    更多:https://laravel.com/docs/master/responses#redirecting-with-flashed-session-data

    【讨论】:

    • 尝试了会话代码,仍然为空..不知道为什么
    猜你喜欢
    • 2014-04-27
    • 2023-03-14
    • 2015-01-11
    • 2016-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-21
    相关资源
    最近更新 更多