Can not I update with Eloquent in laravel 5.6.25?

0

Hi, I'm trying to update Laravel, I'll leave my model and my controller, I'll even show the values in the log and if they are correct. My database is Mysql.

This use in Ajax

$.ajax({
                type: 'PUT',
                url: 'posts/' + id,
                data: {
                    '_token': $('input[name=_token]').val(),
                    'id': $("#id_editar").val(),
                    'orden_de_compra': $('#orden_de_compra_editar').val(),
                    'cliente': $('#cliente_editar').val()
                },
                success: function(data) {
                    $('.errororden_de_compra').addClass('hidden');
                    $('.errorcliente').addClass('hidden');

This is my function:

 public function update(Request $request, $pedido_id)
    {
        error_log($pedido_id);
               error_log($request->orden_de_compra);
               error_log($request->cliente);
           $array=array("pedido_id"=>$pedido_id);



              $post = Pedido::findOrFail($pedido_id);

           $post->cliente =  $request->cliente;

           $post->orden_de_compra = $request->orden_de_compra;
$post->save();

            return response()->json($array);

    }

and this my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Pedido extends Model
{
   //evita la s para la tabla:  protected $table = 'table_name';


        protected $primaryKey = 'pedido_id';
        protected $fillable = ['orden_de_compra','cliente'];
}
    
asked by Daniel Treviño 06.07.2018 в 06:11
source

3 answers

0

I greet you and I tell you that you should do it this way

  $post = Pedido::findOrFail($pedido_id);
  $post->cliente =  $request->input('cliente');
  $post->orden_de_compra = $request->input('orden_de_compra');
  $post->save();
  

That is to say you must use the input method and inside quotes between   the name of the input in your HTML that contains the value you want   store in your BD

Here more reference in the documentation link

    
answered by 06.07.2018 в 06:15
0

first make sure that the id received is the correct one .. eloquent has the update method, if the inputs of your form are the same as those of your bd, you can do something like this

$post = Pedido::findOrFail($pedido_id);    
$post->update($request->all());
return ...

If you inject the dependency of the Order class, and you use the same object in the route, you save the query

    
answered by 06.07.2018 в 07:21
0

Ready, solve it now add public $ timestamps = false; to my model, this is because I do not have the column updated_at which expects eloquent by default.

    
answered by 07.07.2018 в 05:13