SQLSTATE[42S22]:找不到列:在 laravel livewire 项目中

SQLSTATE[42S22]: Column not found: in laravel livewire project

我正在尝试将导航菜单与导航类型同步,但出现错误 Illuminate\Database\QueryException SQLSTATE[42S22]: Column not found: 1054 Unknown column ' navigation_menu_id' in 'field list' (SQL: insert into 'navigation_menus_navigation_types' (' navigation_menu_id', 'navigation_type_id') values (1, 1))

我不确定为什么找不到 navigation_menu_id。我正在使用带有自定义外键 ID 的自定义数据透视表 table。

*Migration.php

Schema::create('navigation_menus_navigation_types', function (Blueprint $table) {
$table->id('navigation_menus_navigation_types_id');

$table->unsignedBigInteger('navigation_menu_id');
$table->foreign('navigation_menu_id')->references('navigation_menus_id')->on('navigation_menus');

$table->unsignedBigInteger('navigation_type_id');
$table->foreign('navigation_type_id')->references('navigation_menu_types_id')->on('navigation_menu_types');
$table->timestamps(); });

导航菜单Model.php

class NavigationMenu extends Model {
protected $primaryKey = 'navigation_menus_id';

public function navigationType()
{
    return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id',' navigation_menu_id');
}}

导航菜单类型Model.php

class NavigationMenuType extends Model{
protected $primaryKey = 'navigation_menu_types_id';

public function navigationMenu()
{
    return $this->belongsToMany(NavigationMenu::class,'navigation_menus_navigation_types',' navigation_menu_id','navigation_type_id');
}}

PagesNavigation Controller.php

public function syncNavtypes(){
        $this->seletedNavigationMenu = NavigationMenu::find($this->navMenuId);
        $this->seletedNavigationMenu->navigationType()->sync($this->navTypeId);
        $this->modelSyncNavigationTypesVisible = false;
        $this->reset();
        $this->resetValidation();}

我认为你们关系中的问题:

public function navigationType()
{
    return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id',
' navigation_menu_id');  // here!!
}

问题是您在列名的开头添加了额外的 space, 目前 laravel 会认真对待!!

您只需删除多余的 space。

public function navigationType()
{
    return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id',
'navigation_menu_id');  
}

如果没有其他问题,问题已解决。你会在你的关系中添加一个额外的 space -

' navigation_menu_id'

return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id','navigation_menu_id');