SQLSTATE [42S22]:找不到列:在 laravel livewire 项目中
Posted
技术标签:
【中文标题】SQLSTATE [42S22]:找不到列:在 laravel livewire 项目中【英文标题】:SQLSTATE[42S22]: Column not found: in laravel livewire project 【发布时间】:2021-12-20 12:41:25 【问题描述】:我正在尝试将导航菜单与导航类型同步,但我收到错误 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 的自定义数据透视表。
*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(); );
NavigationMenu 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();
【问题讨论】:
投票结束这个问题,因为它是一个错字,在你的NavigationMenu
模型中有一个额外的空间' navigation_menu_id'
【参考方案1】:
我认为你们关系中的问题:
public function navigationType()
return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id',
' navigation_menu_id'); // here!!
问题是您在列名的开头添加了额外的空格, 目前 laravel 会信守承诺!!
你只需要删除多余的空间。
public function navigationType()
return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id',
'navigation_menu_id');
【讨论】:
【参考方案2】:如果没有其他问题,则问题已解决。你会在你的关系中增加一个空间 -
' navigation_menu_id'
return $this->belongsToMany(NavigationMenuType::class,'navigation_menus_navigation_types','navigation_type_id','navigation_menu_id');
【讨论】:
谢谢!我没有意识到我在那里有一个空间 那是输入错误。 :)以上是关于SQLSTATE [42S22]:找不到列:在 laravel livewire 项目中的主要内容,如果未能解决你的问题,请参考以下文章
Laravel 删除数据 - SQLSTATE [42S22]:找不到列:1054 未知列
SQLSTATE [42S22]:找不到列:1054 未知列 - Laravel
SQLSTATE [42S22]:找不到列:1054 未知列 '0' where 子句
SQLSTATE [42S22]:找不到列:1054 未知列 laravel 5.1