0

I have a problem when i´m traying insert data in my DB. always return a message Array to string conversion and i don´t know why. In other controller i have this code and it´s ok. And now i need to insert a data but i can´t

My actual code is:

public function insertarActuacion(){
    $fechaActual = date("Y-m-d H:i:s");
    $tiempoEmpleado = 0;
    $bono = \DB::select(\DB::raw("SELECT codBono
                                  FROM bonos
                                  WHERE bonos.usuario = " . \Auth::user()->id));
    $tiempoRestante = 0;

    \DB::insert("INSERT INTO actuacion (fecha, tiempoEmpleado, usuario, bono, tiempoRestanteBono) VALUES (?, ?, ?, ?, ?) ",
                                                                 [ $fechaActual, $tiempoEmpleado, \Auth::user()->id, $bono, $tiempoRestante ] );
}

thanks for help me

1
  • Please debug $bono before inserting, $bono is returning a object collection as far my knowledge. attach your debugged data with this post. Commented Oct 28, 2020 at 13:22

2 Answers 2

1

The answer to this question can actually already lay in the error message.

One of your inserted values is considered to be an array, but the code tries to cast it as a string, which causes the error.

Check which of your data is an array, and only get the value from it which you need.

Alternatively, if you don't know what data is an array, you can use var_dump($var), which will tell you what the type is and what it's content is. Use that for debugging purposes only.

Sign up to request clarification or add additional context in comments.

Comments

1

As you are inserting $bono is an array. you need to select the value as below.

$bono = \DB::table('bonos')->select('codBono')->where('usuario',\Auth::user()->id)->first();

$codBono = '';
if(!empty($bono)){
   $codBono = $bono->codBono;
}

Now insert this value.

\DB::insert("INSERT INTO actuacion (fecha, tiempoEmpleado, usuario, bono, tiempoRestanteBono) VALUES (?, ?, ?, ?, ?) ",
                                                                 [ $fechaActual, $tiempoEmpleado, \Auth::user()->id, $codBono, $tiempoRestante ] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.