I'm building a REST API using Laravel and wondering if there's a standard and accepted way (Framework or Package) to return complex collections as the response.
What I'm currently doing is, returning the object with json method,
public function show() {
$ad = Ad::with('category')->get()->find($id);
return Response::json($ad);
}
Or, just return the object where it automatically return as json
public function show() {
$ad = Ad::with('category')->get()->find($id);
return $ad;
}
This way is fine with the above method because relations are pre-contained before coveting to json. However, let's say I need to return the saved object just after store() is called. Like this...
public function store(SaveAdRequest $request){
$ad = new Ad();
$ad->title = $request->title;
$ad->description = $request->description;
$ad->category = $request->category;
$ad->save();
return $ad;
}
In above store() method, I have no direct way to get relations of the just saved object.
So, how to overcome this problem and is there a standard way of returning these complex objects rather than using json ?
Thanks!