尝试将变量传递给视图,但视图中未显示任何内容-Laravel [英] Trying to pass a variable to a view, but nothing is shown in the view - laravel

查看:73
本文介绍了尝试将变量传递给视图,但视图中未显示任何内容-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将变量传递给视图,但视图中未显示任何内容.

Trying to pass a variable to a view, but nothing is shown in the view.

控制器代码:

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

推荐答案

您无法通过重定向发送变量.

You can't send variable with redirect.

尽管您可以通过会话传递某些数据,例如follow.

Although you can pass session with some data like follow.

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

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

And then you can fetch the data from blade template using:

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

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

这篇关于尝试将变量传递给视图,但视图中未显示任何内容-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆