LaravelFly
LaravelFly

Reputation: 53

How to simplify multi search in Laravel?

Sometimes such a scene when we use where in Laravel, like this:

if (!empty($request->input('user_name'))) {
    $where[] = ['user_name', 'like', "%" . $request->input('user_name') . "%"];
}
if (!empty($request->input('group_id'))) {
    $where[] = ['group_id', $request->input('group_id') ];
}
if (!empty($request->input('email'))) {
    $where[] = ['email', 'like', "%" . $request->input('email') . "%"];
}

if (!empty($request->input('mobile'))) {
    $where[] = ['mobile', 'like', "%" . $request->input('mobile') . "%"];
}
$users = User::where($where)->get();

but its so ugly, but I just want use the this User::search($request->only(['user_name', 'email', 'mobile']));, maybe we have to design some rules for the key name of the input name and do you have some good idea for this condition? Thanks.

Upvotes: 4

Views: 278

Answers (3)

LaravelFly
LaravelFly

Reputation: 53

For the convenience of search, I slightly improved it like this:

//use style
User::search(['eq'=>$request->only(['is_messaged', 'is_mailed', 'status']), 'like' => $request->only(['user_name', 'mobile'])]);
//the implement method
public function scopeSearch($query, $conditions)
{
    $where = [];

    foreach ($conditions as $key => $condition) {
        if ($key == 'like') {
            foreach ($condition as $whereKey => $whereValue) {
                if ($whereValue == '') continue;
                $where[] = [$whereKey, 'like', $whereValue."%"];
            }
        }
        if ($key == 'eq') {
            foreach ($condition as $whereKey => $whereValue) {
                if ($whereValue == '') continue;
                $where[] = [$whereKey, $whereValue];
            }
        }
    }

    return $query->where($where);
}

Upvotes: 1

Rohit Dalal
Rohit Dalal

Reputation: 866

You Can do it without loop as follows

$string = 'CASE WHEN '".$request->input('user_name')."' != '' THEN user_name like "%".'".$request->input('user_name')."'."%" WHEN $request->input('group_id') != '' THEN group_id = '".$request->input('group_id')."' CASE WHEN '".$request->input('email')."' != '' THEN email like "%".'".$request->input('email')."'."%" CASE WHEN '".$request->input('mobile')."' != '' THEN mobile like "%".'".$request->input('mobile')."'."%" ';

$users = User::whereRaw($string)->get();

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163788

You can create local scope:

public function scopeSearch($q, $inputs)
{
    $where = [];

    foreach ($inputs as $key => $data) {
        $where[] = [$key, 'like', "%".$data."%"];
    }

    return $q->where($where);
}

And then use it:

User::search($request->only(['user_name', 'email', 'mobile']))->get();

Upvotes: 6

Related Questions