mardi 11 septembre 2018

Laravel change request input from string to array

I'm building an API with custom query filter, in my filter, there are rules that value cannot be empty and some fields has to be an array.

I have managed to filter out empty fields if they are submitted but I can't convert request input to the array, is there a way to do it?

Here is my code:

public function removeEmptyFieldsFromRequest($request)
{

    $emptyFields = [];

    foreach ($request->all() as $name => $value)
    {
        if (is_null($value)){
            $emptyFields[] = $name;
        }


        $fields = ['transmissions', 'grades', 'colors', 'equipment', 'lots', 'chassis', 'auctions', 'models'];

        if (in_array($name, $fields)){

           // here need to convert request value from a string into the array
        }


    }

    $request = $request->except($emptyFields);

    return $request;
}

I want to make this filter usable in different cases, I know that I can change the input name to the array on the front end



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2NAYrtk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire