【问题标题】:Laravel 5 how to return a template with response()->json()?Laravel 5 如何使用 response()->json() 返回模板?
【发布时间】:2015-09-15 00:39:55
【问题描述】:

下面是我的数据库:

post:
id user_name content created_at

下面是我的后期控制器:

public function new(Request $request){
        $data = $request->all();
        $post = new Post;
        $post->user_name = $data['name'];
        $post->message = $data['content'];
        $post->save();
        $id = $post->id;
        $show = Post::find($id)->first();
        // $html = view('wall.post')->with('post', $show);
        return response()->json(['success' => $post->id,'message'=> 'Post Sent', 'html' => $html]);
    }

还有我的 /views/wall/post.blade.php :

<div class="ui post">
<div class="name">User : {{ $post->user_name }}</div>
<div class="content">{{ $post->content }}</div>
</div>

下面是我的问题:

我如何使用 user_namepost.blade.php 获取内容和 当我发布 name=kenny 和 content=testing 时,content 返回到 new() 中的 $html

<div class="ui post">
<div class="name">User : Kenny</div>
<div class="content">Testing</div>
</div>

我只是使用 jquery 将上述代码添加到我的页面中?

谢谢

{对不起,如果我的英语语法不好}

【问题讨论】:

    标签: php jquery mysql json laravel-5.1


    【解决方案1】:

    你可以像这样渲染一个视图:

    <?php
    $html = view('wall/post')->render();
    

    【讨论】:

    • 感谢@Joachim Martinsen 我使用如下,它可以工作。 $data = Post::where('id',$id)-&gt;first(); $html = view('wall.post')-&gt;with('post', $data)-&gt;render();
    猜你喜欢
    • 2015-08-08
    • 2018-05-31
    • 1970-01-01
    • 2016-01-21
    • 2018-10-11
    • 2014-03-09
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多