How to enter values in Laravel

Asked

Viewed 200 times

1

I am 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 is returning the following message:

Object of class stdClass could not be converted to string (SQL: INSERT INTO table (field 1, field 2, field 2) values(:field 1, :field 2, :field 3)

I don’t know I tried to change the insert for 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]

inserir a descrição da imagem aqui

1 answer

1


In the Laravel how to use it 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);

by your question you 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 make a Insert for Laravel.

Reference:

  • The error message continues

  • @adventistaam put in your question your true code! what proposed to you is through the documentation has no way to err!

  • Ready added the real code

  • @adventistaam paste the image of error in your question!

  • I’ve already added the image

  • then the mistake is in Str where you use it? @adventistaam

  • Sorry I didn’t understand your question

  • The problem is not in that code @adventistaam where you use Str???

  • In fact the only str command I use is that sql command

Show 4 more comments

Browser other questions tagged

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