lundi 14 novembre 2016

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('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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire