简体   繁体   中英

Return a JSON Array to a View in Laravel

I am using Laravel 4 to build an applicaiton.

This is my code:

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');
    }

The issue is rather than returning a message I would like to return a json array to the view with a success code and the name and body of $candle . Can anyone advise me on how to do this?

Instead of using save() function you can use create() function to create a new row. Create() function returns you the last inserted row, which you can use to show it in your message.

Example:

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

Please refer: http://laravel.com/docs/eloquent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM