More than one array value for Where Eloquent

Asked

Viewed 87 times

1

I’m developing a route for my api, where it is intended to return a list of cities based on the fields and values of my array. Thus:

public function arrayCidades(Request $request)
{
    $ay      = $request->ay;
    return $ay;

    $result = Cidade::where($ay)->get();
    return $result;
}

Keep going like I’m going through postman, and the return it gives me of first question:

postman

When commenting on the line return $ay;, returns me the following error sql:

"message": "SQLSTATE[HY093]: Invalid Parameter number: Parameter was not defined (SQL: select * from "cities" Where ("id_state" = 15 and "name_city" = 5))"

My goal here would be an sql like this:

select * from cidades where id_estado in(15,5) and nome_cidade = 'Santa Luzia'

Does anyone know how to solve?

Laravel 7.14.1

  • You want to pass the data that comes in the ARRAY format ... ? this has to be worked in the back-end, I did not understand which is your filter, is for all fields or is some in specific.

  • Missing say what you want !

  • The array name will always be by default ay. The name of your fields will always be some name that references the table, for example id_estado and nome_cidade.

  • Why do you want to do this? What type of filter you want to make, I think you have a better way to drive this filter

  • What are the filters? or do you want something dynamic? explain better

  • Filters are the names of the array fields, which refer to table fields

  • only that you have duplicity in the fields because they are arrays ... what is the purpose?

  • There is duplicity because I want to pass more than one value to the same field understand? When passing ay[id_estado] and ay[id_estado], Each one can assume only one value, right? Until then ok, my controller returns the data and quiet. But if I want to inform more of a different state, with another id_estado for example, and use this in the search. This is my question: If there is how to do it this way, because for me it is already half way

Show 3 more comments

1 answer

1


Directly as it is in the code will not work, have to have a logic, a way to analyze the parameters, example in the code below has been analyzed if the data comes in a array simple and the other if it is a text:

Layout of the data to be sent:

{
    "id":[1,2,51], 
    "nome":"nome"
}

Code on the back end of Laravel:

$model = $this->model; // model do banco

$data = $request->all();
$keys = array_keys($data);        
foreach ($keys as $key) {
    if (gettype($data[$key]) === "array"){
        $model = $model->whereIn($key, $data[$key]);
    } else {
        $model = $model->where($key, $data[$key]);
    }
}
return $model->get();

that is, I checked if the data is a array of a dimension if I do not believe already to be a given text so of the simple if and always update the model with the last expression of builder and finally the result.

Observing: If you have more data, you need to encode a little more to check unique types of your search.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.