Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get absolute value by where condition in laravel?

               public function getList(Request $request)
              {
                 $keyword = $request->keyword;
                 $status = $request->status;
                 $userId = $request->userId;
                 $list = DB::table('user')
                    ->selectRaw("user.id, user.name, user.email, user.status")
                    ->whereIn('user.id', $userId)
                    ->when(!empty($keyword), function ($query) use ($keyword) {
                        $query->where('user.name', 'like', '%' . $keyword . '%');
                        $query->orWhere('user.email', 'like', '%' . $keyword . '%');
                    })
                    ->when(!empty($status), function ($query) use ($status) {
                        $query->where('user.status', '=', $status);
                    });
                    $list->get()->toArray();
              }

In laravel i am doing search function by email, name and status.
The above code worked out..But there is a small problem:

              {
                    "id": 10,
                    "name": "jony",
                    "email": "user1@gmail.con",
                    "createdAt": "05-05-2021 08:43:05",
                    "status": "2"
                },
                {
                    "id": 11,
                    "name": "test",
                    "email": "jony@gmail.com",
                    "createdAt": "05-05-2021 08:43:05",
                    "status": "1",
                }

When I enter keyword with the keyword jony it gets 2 such items, this is correct.

BUT when I set status equal to 1 or equal to 2 the result stays the same.. Is there any way to get the result according to the correct status. Please advise me. Thanks.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The problem is on your orWhere clause, you may want to enclose that to a group (put the query into a parentheses, for more info go HERE).

Here’s what you want to to achieve:

public function getList(Request $request)
{
    $keyword = $request->keyword;
    $status = $request->status;
    $userId = $request->userId;
    $list = DB::table('user')
    ->selectRaw("user.id, user.name, user.email, user.status")
    ->whereIn('user.id', $userId)
    ->when(!empty($keyword), function ($query) use ($keyword) {
        $query->where(function ($query) use ($keyword) {
            $query->where('user.name', 'like', '%' . $keyword . '%');
            $query->orWhere('user.email', 'like', '%' . $keyword . '%');
        });
    })
    ->when(!empty($status), function ($query) use ($status) {
        $query->where('user.status', '=', $status);
    });
    $list->get()->toArray();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading