I want to have a clean code.

Posted 9 months ago by joseluisac


I'm developed a rest api with Laravel.

I have a doubt, i want to develop a foreach for this code, i think that is repeated code, i'll like to have my optimized code.

$estado = $query->pluck('estado')->unique()->values()->all();
$ciudad = $query->pluck('ciudad')->unique()->values()->all();
$municipio = $query->pluck('municipio')->unique()->values()->all();
$colonia = $query->pluck('colonia')->unique()->values()->all();
$cp = $query->pluck('cp')->unique()->values()->all();

$data = [
    'estado' => $estado,
    'ciudad' => $ciudad,
    'municipio' => $municipio,
    'colonia' => $colonia,
        'cp' => $cp,

Can you help me?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.