【问题标题】:Return a JSON Array to a View in Laravel将 JSON 数组返回到 Laravel 中的视图
【发布时间】:2014-08-29 17:58:01
【问题描述】:

我正在使用Laravel 4来构建应用程序。

这是我的代码:

public function handleCreateCandle(){
        $candle = new Candle;
        $candle->name = Input::get('name');
        $candle->body = Input::get('body');
        $candle->user_id = Sentry::getUser()->id;
        $candle->save();
        return Redirect::action('User_BaseController@getPrayerSpace')->with('message', 'Thank you for lighting candle');
    }

问题不是返回消息,而是我想将 json 数组返回到视图,其中包含成功代码以及 $candle 的名称和正文。谁能建议我如何做到这一点?

【问题讨论】:

  • 你是什么意思?你想要一个 json 字符串而不是纯文本吗?然后只使用json_encode? ->with('message', json_encode($candle))

标签: php mysql laravel laravel-4


【解决方案1】:

您可以使用 create() 函数来创建新行,而不是使用 save() 函数。 Create() 函数返回最后插入的行,您可以使用它在消息中显示它。

例子:

$user = User::create(array('name' => 'John'));

请参考: http://laravel.com/docs/eloquent

【讨论】:

    猜你喜欢
    • 2018-03-24
    • 2023-03-20
    • 2013-03-13
    • 2014-03-09
    • 2017-05-24
    • 2013-10-28
    • 2016-06-19
    • 2014-11-23
    • 2020-10-11
    相关资源
    最近更新 更多