首页 > 解决方案 > 取数组值并与其余部分分开

问题描述

我从 eloquent 查询并接收数组中的记录,我需要获取记录中团队的每个值,以验证它是大于还是小于表单中输入的值。

public function listarLojasParaValidarInsercaoHorarioLoja(string $uuidLoja,array $dados) : string
{

    $loja = $this->getByLoja($uuidLoja);
    $idLoja = $loja[0]->getLoja()->getId();

    $diaSemana = $dados['dia_semana'];


    $bancoHorario = HorarioLojaLaravel::where('loja_id', '=', $idLoja)
                        ->where('dia_semana', '=', $diaSemana)->get();
    $contarBanco = $bancoHorario->count();

    if ($contarBanco > 0) {
        $aberturaBanco = HorarioLojaLaravel::where('loja_id', '=', $idLoja)
                            ->where('dia_semana', '=', $diaSemana)
                            ->get('horario_abertura');

        $fechamentoBanco = HorarioLojaLaravel::where('loja_id', '=', $idLoja)
                               ->where('dia_semana', '=', $diaSemana)
                               ->get('horario_fechamento');

        foreach ($aberturaBanco as $key => $value) {

            $horario_abertura = $dados['horario_abertura'];
            $horario_fechamento = $dados['horario_fechamento'];

            if ($horario_abertura >= $aberturaBanco and $horario_abertura <= $fechamentoBanco || $horario_fechamento <= $fechamentoBanco and $horario_fechamento >= $horario_abertura) {
                echo $aberturaBanco;
            } else {
                echo $aberturaBanco;
            }
        }
    } else {
        return False;
    }
}

结果 var_dump()

[{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"09:00:00"},{"horario_abertura":"09:00:00"}][{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"09:00:00"},{"horario_abertura":"09:00:00"}][{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"09:00:00"},{"horario_abertura":"09:00:00"}][{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"09:00:00"},{"horario_abertura":"09:00:00"}][{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"08:00:00"},{"horario_abertura":"09:00:00"},{"horario_abertura":"09:00:00"}]

我需要得到数字

标签: phplaravel

解决方案


我根据您的评论猜到您需要什么,替换->get('horario_abertura')->pluck('horario_abertura')您的代码:

$aberturaBanco = HorarioLojaLaravel::where('loja_id', '=', $idLoja)
                        ->where('dia_semana', '=', $diaSemana)
                        ->pluck('horario_abertura');

并将您的 foreach 更改为:

foreach ($aberturaBanco as $key => $value) {

            $horario_abertura = $dados['horario_abertura'];
            $horario_fechamento = $dados['horario_fechamento'];

            if ($horario_abertura >= $value and $horario_abertura <= $fechamentoBanco || $horario_fechamento <= $fechamentoBanco and $horario_fechamento >= $horario_abertura) {
                echo $value;
            } else {
                echo $value;
            }
        }

另一个更简单的解决方案是什么都不做,用它$value['horario_abertura']来获得你想要的值。

请告诉我它是否解决了您的问题。


推荐阅读