简体   繁体   中英

Backpack for laravel - Error updating rows

I'm trying to update rows of some tables on my admin. It's running with backpack crud. I've doing some debug and I'm ending on this sentence in backpack CrudController (vendor/backpack/crud/src/app/http/controllers/crudController - UpdateCrud function).

$request->get($this->crud->model->getKeyName();

It's giving me a null value when I use dd() on it. So the final result shows me a 404 Error saying 'No query results for model ['Model route']'. Also I print the update request and every field it's ok, sending data without problems.

I spend 6 days trying to resolve this problem and can't find it. This is part of my code, thanks for your time.

Controller (I resume some fields, because are to many)

class OperadoresCrudController extends CrudController {
  public function setup(){
    $this->crud->setModel('App\Models\Operador');
    $this->crud->setRoute("admin/operadores");
    $this->crud->setEntityNameStrings('operadores', 'operadores');
    $this->crud->enableExportButtons();

    /*
    |--------------------------------------------------------------------------
    | BASIC CRUD INFORMATION
    |--------------------------------------------------------------------------
    */

    //$this->crud->setFromDb();

    // ------ CRUD FIELDS
    // $this->crud->addField($options, 'update/create/both');
    // $this->crud->addFields($array_of_arrays, 'update/create/both');
    // $this->crud->removeField('name', 'update/create/both');
    // $this->crud->removeFields($array_of_names, 'update/create/both');

    $this->crud->addField([
      'name'  => 'nombre',
      'label' => 'Nombre *',
      'type'  => 'text',
      'wrapperAttributes' => [
        'class' => 'form-group col-md-7'
      ],
      'tab'  => 'Datos Generales'
    ]);

    $this->crud->addField([
      'name' =>  'rut',
      'label' => 'Rut',
      'type'  => 'text',
      'wrapperAttributes' => [
        'class' => 'form-group col-md-7'
      ],
      'tab'  => 'Datos Generales'
    ]);

    public function store(StoreRequest $request) {
      return parent::storeCrud();
    }

    public function update(UpdateRequest $request) {
      #dd($request);
      return parent::updateCrud();
    }
 }

Model

class Operador extends Model {
  use CrudTrait;

/*
|--------------------------------------------------------------------------
| GLOBAL VARIABLES
|--------------------------------------------------------------------------
*/

  protected $table = 'operador';
  //protected $primaryKey = 'id';
  // public $timestamps = false;
  // protected $guarded = ['id'];
  protected $fillable = ['nombre','rut','direccion','telefono','email','comuna_id','url','region_id', 'area_operacion','op_nombre_responsable','op_direccion_responsable','op_telefono_responsable','op_email_responsable','created_at','updated_at'];
  // protected $hidden = [];
  // protected $dates = [];

/*
|--------------------------------------------------------------------------
| FUNCTIONS
|--------------------------------------------------------------------------
*/
  public function equipos() {
   return   $this->hasMany('App\Models\Equipo','operador_id');
  }

  public function comuna() {
    return $this->belongsTo('App\Models\Comuna');
  }

  //para insertar select region, prov, comuna
  public function region() {
    return $this->belongsTo('App\Models\Region');
  }

  // Estacion Operador.
  public function estaciones() {
    return $this->hasMany('App\Models\Estacion','operador_id');
  }
}

In your EntityCrudController::update() method you should be able to use $this->crud->entry after you call parent::updateCrud() . So this should work for you to get the current entry ID:

```

public function update(UpdateRequest $request)
{
    $redirect_location = parent::updateCrud();

    dd($this->crud->entry->id);

    return $redirect_location;
}

```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM