【问题标题】:Query get api with array用数组查询获取api
【发布时间】:2020-03-29 09:34:27
【问题描述】:

我需要获取信息并将其放入数组中,但出现错误。

在进行 get 查询的类的代码下方

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;
}

型号代码下方

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');
    }
}

错误:

函数 Illuminate\Support\Collection::get() 的参数太少,在第 32 行的 /var/www/html/code/app/Persistencia/Laravel/Repositorios/HorariosLojasRepositorioLaravel.php 中传递了 0,预计至少有 1 个{"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): 函数 Illuminate\Support\Collection::get() 的参数太少,0 传入 /var/www/html /code/app/Persistencia/Laravel/Repositorios/HorariosLojasRepositorioLaravel.php 位于第 32 行,预计至少 1 个位于 /var/www/html/code/vendor/laravel/framework/src/Illuminate/Support/Collection.php:874)

【问题讨论】:

    标签: laravel


    【解决方案1】:

    当您将 all() 与 Eloquent 模型一起使用时,您实际上是在执行查询。

    您只需删除对all() 的调用即可:

    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;
    }
    

    由于将 get() 与 Eloquent 一起使用将返回 collection,因此您甚至可以执行以下操作:

    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();
    }
    

    【讨论】:

    • 出现列错误,我相信正确的不是where而是wherehas。
    • @RichardNicson 抱歉,我什至没有注意到,我只是从您的问题中复制并粘贴了代码……我的错。您是否尝试过 whereHas 的代码并成功了?
    • @RichardNicson 您现在遇到什么错误?
    • "mensagem": "未定义变量:horario",
    • @RichardNicson 如果您要关闭第二个示例,我刚刚更新了它。我忘了用$horario 替换$item
    【解决方案2】:

    收集的方法需要一个值,检查文档:Collection-Get

    所以你不需要这个 ->

    $horariosDaLoja = $query->get();
    

    因为这里

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

    你在 $query 中有你的记录 并且可以使用 ->toArray 方法进行收集,查看toArray

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 2015-10-23
      • 2013-03-06
      • 2022-07-10
      • 1970-01-01
      相关资源
      最近更新 更多