dimanche 18 septembre 2016

Why does laravel default to 'id' as a primary key of a model even when I provide a custom primary key

I have a model like so :

class DataSet extends Model
{
    //
     protected $table = 'data_sets';
     protected $primary_key='iddata_sets';

     protected $fillable=['iduser','path','rows'];

      /**
     * a Dataset belongs to a single user
     *
     * @var array
     */
     public function user()
    {
        return $this->belongsTo('App\User');
    }


     /**
     * a Dataset has many columns
     *
     * @var array
     */
     public function columns()
    {
        return $this->hasMany('App\DataSetColumn','iddata_sets','iddata_sets');//hasMany('model',foreingnkey,localkey)
    }  


}

As you can see I have provided the primary_key variable as 'iddata_sets', But when I try to find a row in the dataset like so ,

    //controller function

    function delete(){


        $iddata_sets=7;
        $pid=1;


        $dataset = DataSet::find($iddata_sets); //DataSet is the model

        $dataset->delete();  
        //DataSet::destroy($iddata_sets); //This doesn't work either

        return redirect('projects/'.$pid); 
    }

It gives me a error as :

Column not found: 1054 Unknown column 'data_sets.id' in 'where clause' (SQL: select * from `data_sets` where `data_sets`.`id` = 7 limit 1)

Not sure why it is assuming id as primary key.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire