Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 1 year ago.
0
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.

Last updated 1 year ago.
0

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

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.
0

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.
0

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

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ulshamim ulshamim Joined 17 May 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.