Home > Software design >  SQLSTATE[42S22]: Column not found: in laravel livewire project
SQLSTATE[42S22]: Column not found: in laravel livewire project

Time:11-07

I am trying to sync the navigation menu with navigation type but I'm getting the error 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))

I am not sure why navigation_menu_id is not found. I am using a custom pivot table with custom foreign key 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');
}}

Navigation Menu Type 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();}

CodePudding user response:

I think the problem in your relation:

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

the thing is that you have added extra space to the begin of the column name, currently laravel will take it to the letter!!

you have to just remove the extra space.

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

CodePudding user response:

Problem is fixed if there's no additional issue. You'd put an extra space in your relation -

' navigation_menu_id'

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