Jack The Baker
Jack The Baker

Reputation: 1883

Laravel api search function with multiple params

I almost read all article in google and posts in stackoverflow, but I couldn't find the right solution or I don't get it. anyway, I want to make a search function in Laravel API, so I started with:

public function search(Request $request) {
    $data = $request->get('data');

    $search = Post::where('title', 'like', "%{$data}%")
             ->orWhere('user_id', 'like', "%{$data}%")
             ->get();

    return response()->json([
        'data' => $search
    ]);
}

This working fine, and by below URL return me data:

http://127.0.0.1:8000/api/search?data=22

{
    "data": [
        {
            "id": 10,
            "title": "some",
            "created_at": "2019-11-02 07:56:40",
            "updated_at": "2019-11-02 07:56:40",
            "user_id": "22"
        }
    ]
}

But I want to get data for each parameter like this, and also remove params from url, instead send as form data:

http://127.0.0.1:8000/api/search

form data:

{
user_id: 22
}

So what I tried so far is:

   public function search(Request $request) {
        $user_id = request('user_id'); // this return 22
        $title = request('title');

        $search = Post::where('title', 'like', "%{$title}%")
                 ->orWhere('user_id', 'like', "%{$user_id}%")
                 ->get();

        return response()->json([
            'data' => $search
        ]);
    }

But it return all data, not just data with user_id-> 22, Any idea?

Result:

{
    "data": [
        {
            "id": 8,
            "title": "some",
            "created_at": "2019-11-02 07:55:57",
            "updated_at": "2019-11-02 07:55:57",
            "user_id": "12"
        },
        {
            "id": 10,
            "title": "some",
            "created_at": "2019-11-02 07:56:40",
            "updated_at": "2019-11-02 07:56:40",
            "user_id": "22"
        },
        {
            "id": 11,
            "title": "some",
            "created_at": "2019-11-02 07:56:45",
            "updated_at": "2019-11-02 07:56:45",
            "user_id": "4"
        },
        {
            "id": 12,
            "title": "some",
            "created_at": "2019-11-02 07:56:47",
            "updated_at": "2019-11-02 07:56:47",
            "user_id": "42"
        }
    ]
}

I want it just return id 10 because it only data with user_id 22.

Upvotes: 0

Views: 2986

Answers (2)

Afraz Ahmad
Afraz Ahmad

Reputation: 5386

I use post request when multiple parameters are being used.

Then in function I filter data like shown below:

public function searchFilters($request){
    $query = User::query();
    if($request->has('name')){
        $query = $query->where('title','like','%'.$request->name.'%');
    }
    if($request->has('salary_negotiable')){
        $query = $query->where('negotiable',$request->salary_negotiable);
    }
    if($request->has('city_ids')){
        $query = $query->whereIn('city_id',$request->city_ids);
    }
    if($request->has('experience_level_ids')){
        $query = $query->whereIn('experience_level_id',$request->experience_level_ids);
    }
    if($request->has('job_type_ids')){
        $query = $query->whereIn('job_type_id',$request->job_type_ids);
    }
    if($request->has('language_name')){
        $query = $query->whereHas('jobLanguageIds',function ($q) use($request){
            $q->select('languages.id','languages.name')->where('languages.name',$request->language_name);
        });
    }
    return $query;
}

This function is just returning a $query instance so that I can re-use and append more queries where ever I want. Be careful though with appending orWhere[...] constraints as they may overrule all the other conditions if you don't wrap them in an overall where().

You can also change return $query to return $query->get(); if you want the actual result from the database straight away.

Upvotes: 1

Jordan Lipana
Jordan Lipana

Reputation: 447

Fixed the issue by changing ->orWhere to ->where to make the condition as AND.

Upvotes: 4

Related Questions