How to enter values in Laravel

1

I'm trying to use the following command to insert:

 $sql = "INSERT INTO tabela (campo1, campo2, campo2, campo4)
                     values(:campo1, :campo2, :campo3,'S')";
        $campos = array(
            ':campo1'      => $nome,
            ':campo2'      => $endreco,
            ':campo3'      => $bairro
        );
        $insert = DB::insert( $sql, $campos );

But you are returning the following message:

  

Object of class stdClass could not be converted to string (SQL: INSERT   INTO table (field1, field2, field2) values (: field1,: field2,: field3)

I do not know, I tried to change insert through select and nothing

Can you help me?

[EDIT 1]

public function salvar( Request $request ){

        $solicitante = $request->input( 'solicitante' );
        $usuario     = $request->input( 'usuario' );
        $setor       = $request->input( 'setor' );
        $descricao   = $request->input( 'descricao' );
        $ramal       = $request->input( 'ramal' );
        $observacao  = $request->input( 'observacao' );
        $codigo      = $this->proxRegistro();

        $sql = "insert into abertura_chamado 
                 (cd_os, dt_pedido, ds_servico, ds_observacao, nm_solicitante,TP_SITUACAO, CD_SETOR,
                  CD_MULTI_EMPRESA, CD_TIPO_OS, NM_USUARIO, DT_ULTIMA_ATUALIZACAO,
                  SN_SOL_EXTERNA, CD_OFICINA, SN_ORDEM_SERVICO_PRINCIPAL, 
                  SN_PACIENTE, DT_ENTREGA, TP_PRIORIDADE, SN_RECEBIDA,  SN_ETIQUETA_IMPRESSA,
                  SN_EMAIL_ENVIADO, TP_CLASSIFICACAO, CD_ESPEC, DS_RAMAL, TP_LOCAL
                 )
                 values 
                ( ?, SYSDATE, ?, ?, ?, 'S', ?, 
                 1, 30, ? , SYSDATE , 
                 'S', 14, 'S',  
                 'N', SYSDATE , ?, 'N',  'N',
                 'N',  'P', 31, ?, 'I')";
        $prioridade = 'E';
        $campos = array(
             $codigo,
             $descricao,
             $observacao,
             $solicitante,
             $setor,
             $usuario,
             $prioridade,
             $ramal

        );


        DB::insert( $sql, $campos );


    }

[EDIT2]

    
asked by anonymous 06.03.2018 / 20:18

1 answer

1

No Laravel how to use follows the example:

DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);

In your case:

$sql    = "INSERT INTO tabela (campo1,campo2,campo3) values(?,?,?)";
$campos = array($nome,$endreco,$bairro);
$insert = DB::insert($sql,$campos);

Your question may have confused and could do so too:

DB::table('tabela')->insert(
    [
         'campo1' => $nome,
         'campo2' => $endereco,
         'campo3' => $bairro
    ]
);

That is, there are some ways to Insert by Laravel .

06.03.2018 / 22:20