skip to Main Content

i have a problem, how to get there are multiple in database values.

I used this method it didn’t work :

Route::get('/users/{id}', function ($id) {
    $user = User::where('id', [$id])->get();
    return $user;
});

I used this method successfully :

Route::get('/users/{id}', function ($id) {
    $user = User::where('id', [1, 2])->get();
    return $user;
});

i want to display the data i want for example 1,2 or 1,3 or 2,1 inside urls

http://127.0.0.1:8000/users/1,2
http://127.0.0.1:8000/users/1,3
http://127.0.0.1:8000/users/2,1

I’m still a newbie in Laravel, who knows someone can help with my problem. thanks

2

Answers


  1. Below code definitely work for you.

    Route::get('/users/{ids}', function ($ids) {
        $id_array = explode(',', $ids);
        $users = User::whereIn('id', $id_array)->get();
        return $users;
    });
    Login or Signup to reply.
  2. You’re getting the ids as a comma-separated string.

    Route::get('/users/{id}', function ($id) {
        $userIds = explode(',', $id);
        $user = AppModelsUser::whereIn('id', $userIds)->get();
        return $user; });
    

    user the above code to convert it to an array then you can get results with multiple users.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search