简体   繁体   中英

fetch multiple records for a same user id in laravel5.4

I am writing function for webservices in laravel5.4. My input to the function is a user id and want to load all record which matches the user id in the Test table. Test table will have multiple record/test for the same userid. this below function returns only one user id record.Any suggestions on how to fetch multiple test records for same user id.For example,I have 30 test records for same userid 2.

public function LoadTest(Request $request)
{
    $response = array();
    $response['status'] = 'error';

    if(($request->has('userid')) && ($request->userid))
    {
        $userid= trim($request->input('userid'));

        $checkuserid= DB::table('user')
                      ->where('user_id',$userid)
                      ->get();

        if(!empty($checkuserid) && $checkuserid->count() > 0)  {

            $test= Test::find($userid);
            return response()->json($test); 

        } else {
            $response['message'] = 'Please enter valid user id.';
        }          

    }
    return response()->json($response);         
}

这样使用

$test= Test::where('userid', $userid)->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM