Error passing array as parameter for Eloquent object

Asked

Viewed 109 times

0

I’m having trouble inserting a type 1:N record using Eloquent. When I pass the array with attributes to the object as the documentation guides an error is launched and when I print the error with echo $ex->getMessage() the only thing that is printed is "number", which is the index I give to the array I am going through. 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 Trader object is being registered in the bank but your phone numbers are not.

1 answer

2

It seems that the saveMany phone is not correct. Try it this way:

$cambista->telefones()->saveMany([
    new \Telefone(["numero" => "(82) 99178-1066"]),
    new \Telefone(["numero" => "(82) 99303-9037"]);
]);
  • Still gives the same error. Thanks for responding.

  • Solved? ?

  • 1

    I set the method in the Cambista model to: public Function phones() { Return $this->hasMany(' Phone', 'idCambista', 'id'); } Apparently the order of the keys was wrong since I have to override the Eloquent pattern for having different names than he expects. But the strangest thing is that the error launched did not tell me anything. Anyway I really appreciate the answer. I hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.