Laravel 删除数据 - SQLSTATE[42S22]: 未找到列:1054 未知列

Laravel delete data - SQLSTATE[42S22]: Column not found: 1054 Unknown column

如何在从数据库中删除数据时解决 laravel 中的外键名称问题。我在 class 文件中更改了关系中的名称,但仍然无效。我需要在哪里进行更改? "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'kontakt.id' in 'where clause' (SQL: select * from kontakt where kontakt.id = 1 limit 1)"

class User extends Model
{
    public $table = "user";
    public function kontakt(){

     return $this->hasMany('App\kontakt');
    }
}


class kontakt extends Model
{
    public $table = "kontakt";
    public function user(){

        return $this->belongsTo('App\User','id_user','id_kontakt');
    }
}
Schema::create('user', function (Blueprint $table) {
            $table->bigIncrements('id_user');
            $table->string('imie_user');
            $table->string('nazwisko_user');
            $table->string('haslo_user');
            $table->string('email')->unique();
}
Schema::create('kontakt', function (Blueprint $table) {
            $table->bigIncrements('id_kontakt');
            $table->string('imie_kontakt');
            $table->string('nazwisko_kontakt');
            $table->string('opis_kontakt');
            $table->string('miasto_kontakt');
            $table->string('ulica_kontakt');
            $table->string('nr_bloku_kontakt');
            $table->string('nr_mieszkania_kontakt');
            $table->string('telefon_kontakt');
            $table->string('nr_konta_kontakt');
            $table->string('nip_kontakt');
            $table->unsignedBigInteger('id_user');
            $table->foreign('id_user')->references('id_user')->on('user');
        })


 public function deletekontakt($id_kontakt){

    $kontakt = kontakt::find($id_kontakt);
    $kontakt->delete();
    return redirect('kontakty');
}

Eloquent 还将假设每个 table 都有一个名为 id 的主键列。您可以定义 $primaryKey 属性 来覆盖此约定。

在您的 kontakt 模型中定义主键:

class kontakt extends Model
{
    public $table = "kontakt";
    protected $primaryKey = 'id_kontakt';

    public function user(){

        return $this->belongsTo('App\User','id_user','id_kontakt');
    }
}

也在用户模型中。

protected $primaryKey = 'id_user';

删除也可以使用destroy()方法

kontakt::destroy($id_kontakt);