added Response::eloquent method.
This commit is contained in:
parent
13638467b4
commit
9ed91af1e8
|
@ -69,6 +69,7 @@ ## Laravel 3.2
|
||||||
- New, driver based authentication system.
|
- New, driver based authentication system.
|
||||||
- Added Input::json() method for working with applications using Backbone.js or similar.
|
- Added Input::json() method for working with applications using Backbone.js or similar.
|
||||||
- Added Response::json method for creating JSON responses.
|
- Added Response::json method for creating JSON responses.
|
||||||
|
- Added Response::eloquent method for creating Eloquent responses.
|
||||||
|
|
||||||
<a name="upgrade-3.2"></a>
|
<a name="upgrade-3.2"></a>
|
||||||
## Upgrading From 3.1
|
## Upgrading From 3.1
|
||||||
|
|
|
@ -245,6 +245,22 @@ function array_pluck($array, $key)
|
||||||
}, $array);
|
}, $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform Eloquent models to a JSON object.
|
||||||
|
*
|
||||||
|
* @param Eloquent|array $models
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
function eloquent_to_json($models)
|
||||||
|
{
|
||||||
|
if ($models instanceof Eloquent)
|
||||||
|
{
|
||||||
|
return json_encode($models->to_array());
|
||||||
|
}
|
||||||
|
|
||||||
|
return json_encode(array_map(function($m) { return $m->to_array(); }, $models));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if "Magic Quotes" are enabled on the server.
|
* Determine if "Magic Quotes" are enabled on the server.
|
||||||
*
|
*
|
||||||
|
|
|
@ -82,7 +82,7 @@ public static function view($view, $data = array())
|
||||||
* Create a new JSON response.
|
* Create a new JSON response.
|
||||||
*
|
*
|
||||||
* <code>
|
* <code>
|
||||||
* // Create a response instance with a view
|
* // Create a response instance with JSON
|
||||||
* return Response::json($data, 200, array('header' => 'value'));
|
* return Response::json($data, 200, array('header' => 'value'));
|
||||||
* </code>
|
* </code>
|
||||||
*
|
*
|
||||||
|
@ -98,6 +98,26 @@ public static function json($data, $status = 200, $headers = array())
|
||||||
return new static(json_encode($data), $status, $headers);
|
return new static(json_encode($data), $status, $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new response of JSON'd Eloquent models.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Create a new response instance with Eloquent models
|
||||||
|
* return Response::eloquent($data, 200, array('header' => 'value'));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param Eloquenet|array $data
|
||||||
|
* @param int $status
|
||||||
|
* @param array $headers
|
||||||
|
* @return Response
|
||||||
|
*/
|
||||||
|
public static function eloquent($data, $status = 200, $headers = array())
|
||||||
|
{
|
||||||
|
$headers['Content-Type'] = 'application/json';
|
||||||
|
|
||||||
|
return new static(eloquent_to_json($data), $status, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new error response instance.
|
* Create a new error response instance.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue