Support the ongoing development of →
Requests Database Eloquent
Last updated 1 year ago.
Route::get('u/{uid}/', function($uid){
        $dataset = DB::table('books')->join('issues','','=','issues.book_id')->where('issues.user_id','=',$uid)->groupBy('')->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.

Last updated 1 year ago.

Hi xgenvn,

Thanks but somehow this is not working. I have got this error: Call to a member function toArray() on a non-object

Last updated 1 year ago.
    function ($uid) {
        try {
            $dataset = DB::table('books')->join(
                function ($join) use ($uid) {
                    $join->on('', '=', 'issues.book_id')
                        ->where('issues.user_id', '!=', $uid);

            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);
Last updated 1 year ago.

You may want to check th $dataset before response, in case it's empty or return null which toArray() will fail.

Last updated 1 year ago.

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 :)

Last updated 1 year ago.

That's really nice you got it worked. Have a nice weekend too.

Last updated 1 year ago.

Sign in to participate in this thread!


Your banner here too?

ulshamim ulshamim Joined 17 May 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.