0

I need to fetch information and bring it into an array, but errors are occurring.

Below the code of the class that makes the get query

public function getByLoja(string $uuid): array
{
    $query = HorarioLoja::all();

    $query = $query->where('loja', function ($loja) use ($uuid) {
        $loja->where('uuid', $uuid);
    });

    $horariosDaLoja = $query->get();

    $saida = [];
    foreach ($horariosDaLoja as $horario) {
        $saida[] = $this->horarioLojaTransformador->paraModelo($horario);
    }

    return $saida;
}

Below the model code

class HorarioLoja extends Model
{
    protected $dates = ['created_at', 'updated_at'];
    protected $table = 'horario_lojas';

    public function loja()
    {
        return $this->belongsTo(Loja::class, 'loja_id', 'id');
    }
}

Error:

Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /var/www/html/code/app/Persistencia/Laravel/Repositorios/HorariosLojasRepositorioLaravel.php on line 32 and at least 1 expected {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in /var/www/html/code/app/Persistencia/Laravel/Repositorios/HorariosLojasRepositorioLaravel.php on line 32 and at least 1 expected at /var/www/html/code/vendor/laravel/framework/src/Illuminate/Support/Collection.php:874)

2 Answers 2

1

When you use all() with an Eloquent model you're actually executing the query.

You can simply remove the call to all() and have:

public function getByLoja(string $uuid): array
{
    $horariosDaLoja = HorarioLoja::whereHas('loja', function ($loja) use ($uuid) {
        $loja->where('uuid', $uuid);
    })->get();

    $saida = [];
    foreach ($horariosDaLoja as $horario) {
        $saida[] = $this->horarioLojaTransformador->paraModelo($horario);
    }

    return $saida;
}

Since using get() with Eloquent will return a collection you could even do something like:

public function getByLoja(string $uuid): array
{
    return HorarioLoja::whereHas('loja', function ($loja) use ($uuid) {
            $loja->where('uuid', $uuid);
        })
        ->get()
        ->map(function ($horario) {
            return $this->horarioLojaTransformador->paraModelo($horario);
        })
        ->toArray();
}
Sign up to request clarification or add additional context in comments.

5 Comments

column error is occurring, I believe the correct would not be where but wherehas.
@RichardNicson Sorry, I didn't even notice that, I'd just copied and pasted the code from your question...my bad. Have you tried the code with whereHas and did it work?
@RichardNicson What's the error you're getting now?
"mensagem": "Undefined variable: horario",
@RichardNicson If you're going off the second example, I've just updated it. I'd forgotten to replace $item with $horario.
0

method get for collection need a value, check doc :Collection-Get

so you don't need this ->

$horariosDaLoja = $query->get();

becouse here

       $query = HorarioLoja::all();
       $query = $query->where('loja', function($loja) use ($uuid) {
            $loja->where('uuid', $uuid);
        });

you have your records in $query and you can use ->toArray method for collection, check toArray

           $query = $query->where('loja', function($loja) use ($uuid) {
            $loja->where('uuid', $uuid);
        })->toArray();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.