Return Json Response

Submitted by egig - 3 years ago

In the official docs : http://laravel.com/docs/responses#special-responses, need to do `Response::json(...)` to create json response. Actually, if the returned value is an array or instance of arrayableinterface or jsonableinterface such as eloquent model, you could just return it, it'll be a json, magically.

//array
Route::get('/request-json-array', function(){
    
    $array = array('foo', 'bar');
    
    //this route should returns json response
    return $array;
    
});

//or eloquent model

Route::get('/request-json-model', function(){
    
    //if 'User' is an eloquent model, this route should returns json response
    return User::all();
    
});

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks