Error passing array as parameter to Eloquent object

0

I'm having trouble inserting a record of type 1: N using Eloquent. When I pass the array with the attributes to the object as the documentation guides an error is thrown and when I print the error with echo $ex->getMessage() the only thing that is printed is "numero", which is the index that I give to the array that I'm going. Does anyone know where the error is?

public static function cadastrar(Request $request, Response $response) {
    try {
        $dadosCambista = $request->getParsedBody();

        $cambista = new \Cambista;
        $cambista->nome = $dadosCambista['nome'];
        $cambista->flagAtivo = $dadosCambista['flagAtivo'];
        $cambista->limiteDiario = $dadosCambista['limiteDiario'];
        $cambista->limiteIndividual = $dadosCambista['limiteIndividual'];
        $cambista->limiteCasadinha = $dadosCambista['limiteCasadinha'];
        $cambista->comissao1 = $dadosCambista['comissao1'];
        $cambista->comissao2 = $dadosCambista['comissao2'];
        $cambista->comissao3 = $dadosCambista['comissao3'];
        $cambista->login = $dadosCambista['login'];
        $cambista->senha = md5(SALT . $dadosCambista['senha']);
        $cambista->idRegional = $dadosCambista['idRegional'];
        //$cambista->save();

        foreach ($dadosCambista['telefones'] as $key => $numero) {
            $telefones[$key] = new \Telefone(array("numero" => $numero));
        }

        $cambista->telefones()->saveMany($telefones);

        $meta = Helper::retornaMetaArray(Enum::SUCS_STS, Enum::CREATED, 201);

        return $response->withCustomJson(null, $meta);
    } catch (Exception $ex) {
        $meta = Helper::retornaMetaArray(Enum::ERR_STS, $ex->getMessage(), 400);

        return $response->withCustomJson(null, $meta);
    }
}

This is what is being sent in the body of the message.:

{
    "nome": "Jon Snow",
    "login": "jon",
    "senha": "ghost",
    "flagAtivo": "1",
    "limiteDiario": "500.00",
    "limiteIndividual": "50.00",
    "limiteCasadinha": "100.00",
    "comissao1": "5",
    "comissao2": "7",
    "comissao3": "10",
    "idRegional": "1",
    "telefones": ["(82) 99178-1066", "(82) 99303-9037"]
}

The Currency Exchange object is being registered in the bank, but its telephone numbers are not.

    
asked by anonymous 22.08.2017 / 22:30

1 answer

2

It seems like saveMany of the phone is not correct. Please try the following:

$cambista->telefones()->saveMany([
    new \Telefone(["numero" => "(82) 99178-1066"]),
    new \Telefone(["numero" => "(82) 99303-9037"]);
]);
    
23.08.2017 / 00:57