dimanche 25 novembre 2018

Laravel variable to where clause

I am trying to extend one app to use my new Laravel app. In this scenario I am getting an unknown number of filters and I would like to forward them all to where() clause.

I have made something like this:

private function filterConverter($filter)
{
    $f = [];
    foreach ($filter as $singleFilter) {
        $operator = $this->filterValues[$singleFilter['operator']];
        $value = $operator == 'like' ? '%' . $singleFilter['value'] . '%' : $singleFilter['value'];

        $f[] = $singleFilter['field'] . ',' . $operator . ',' . $value;
    }

    return $f;
}

The thing is that I am getting operators like EQUALS and CONTAINS so I need to convert them to = and LIKE.

With this code I am trying to do this:

return response(MyModel::where($filter)->get());

But it doesn't work. Is there any elegant way to resolve this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire