Column not found: 1054 Unknown column 'id' in 'where clause' Laravel 5.3

3

Greetings, I am updating a table called tbl_articles using the same view where the Primary key = idArticulo, the insert performs me impeccably the problem is when I want to update it generates the error I AM WORKING WITH LARAVEL 5.3

QueryException in Connection.php line 770:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where 
clause' (SQL: update 'tbl_articulo' set 'idArticulo' = 7, 'descripcion' = 
CASACA DAMA 222, 'modelo' = CORTE PRINCESA, 'detalleAdicional' = CASACA DAMA 
2222, 'habilitado' = 1, 'idCategoria' = 2, 'idColor' = 1, 'updated_at' = 
2017-05-23 03:30:42 where 'id' is null) 

It tells me that the id key is not null but my primary key is idArticle. Thanks for your help. Very grateful, I say goodbye.

This is my repository

<?php
namespace App\Repository;
use App\Articulo;
class ArticuloRepository 
{
    protected $primaryKey = 'idArticulo';

    Public function guardar($data){
      $Articulo = new articulo();

      //logica para ver si es un UPDATE
      if ($data['idArticulo'] > 0) 
      {
        $Articulo->exists = true;
        $Articulo->idArticulo = $data['idArticulo'];
      }

      $Articulo->descripcion      = $data['descripcion'];
      $Articulo->modelo           = $data['modelo'];
      $Articulo->detalleAdicional = $data['detalleAdicional'];
      $Articulo->habilitado       = $data['habilitado'];
      $Articulo->idCategoria      = $data['idCategoria'];
      $Articulo->idColor          = $data['idColor'];

      $Articulo->save();
    }
}

This is my driver

<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Repository\ArticuloRepository;

class ArticuloController extends Controller{

    private $ArticuloRepo;
    public function store(Request $request){
        $this->validate($request,[
            'descripcion' => 'required|min:4',
            'modelo' => 'required|min:4',
            'idCategoria' => 'required|integer',
            'idColor' => 'required|integer',
            'habilitado' => 'required|integer',
            'detalleAdicional' => 'required',
            ]);
        $this->ArticuloRepo->guardar($request);
        return redirect('articulo');
    }

    public function create($idArticulo = 0){
        return view('articulo/create',
        ['model'=>($idArticulo > 0 ? $this->ArticuloRepo->obtener($idArticulo):null) ]);

    }

    public function edit($idArticulo = 0){
        return view('articulo/create',
        ['model'=>($idArticulo > 0 ? $this->ArticuloRepo->obtener($idArticulo):null) ]);
    }
}
    
asked by Daviss9 23.05.2017 в 05:56
source

1 answer

3

Although we do not see the class code articulo , apparently you are not telling this that your primary key is idArticulo as you are doing in class ArticuloRepository .

Assuming that your class articulo follows the basic Laravel syntax for the models, you should have something like that in that class:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Articulo extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'tbl_articulo';

    protected $primaryKey = 'idArticulo';

    // etc...

}

More information in the documentation: link

    
answered by 23.05.2017 / 06:10
source