为什么laravel默认为' id'作为模型的主键,即使我提供自定义主键

时间:2016-09-18 15:16:23

标签: php mysql laravel-5 eloquent

我有一个这样的模型:

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)
    }  


}

如您所见,我已将primary_key变量提供为' iddata_sets', 但是当我试图在数据集中找到一行时,

    //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); 
    }

它给我一个错误:

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

不确定为什么假设id为主键。

由于

0 个答案:

没有答案