Route::get('u/{uid}/', function($uid){
try{
$dataset = DB::table('books')->join('issues','books.id','=','issues.book_id')->where('issues.user_id','=',$uid)->groupBy('books.id')->get();
return Response::json($dataset->toArray(),200);
}catch(\Illuminate\Database\Eloquent\ModelNotFoundException $e){
return Response::json('ERROR: ModelNotFound',400);
}catch(Exception $e){
return Response::json('ERROR: '.$e->getMessage(),400);
}
});
Hope this help. Not really tested it, but I think it's really near to what you want to achieve.
Hi xgenvn,
Thanks but somehow this is not working. I have got this error: Call to a member function toArray() on a non-object
Route::get(
'u/{uid}/',
function ($uid) {
try {
$dataset = DB::table('books')->join(
'issues',
function ($join) use ($uid) {
$join->on('books.id', '=', 'issues.book_id')
->where('issues.user_id', '!=', $uid);
}
)->groupBy('books.id')->get();
return Response::json($dataset->toArray(), 200);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return Response::json('ERROR: ModelNotFound', 400);
} catch (Exception $e) {
return Response::json('ERROR: ' . $e->getMessage(), 400);
}
}
);
You may want to check th $dataset before response, in case it's empty or return null which toArray() will fail.
OK I got it. Actually $dataset already returned an array, So we don't need to use toArray().
Thank you very much for your valuable response. Have a nice day :)
That's really nice you got it worked. Have a nice weekend too.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community