Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sandersjj's avatar

Can't open resource because of database error: "Unknown Column"

Hey I am kind of stuck: I have this migration

Schema::create('product_packagings', function (Blueprint $table) {    
 $table->id();     
 $table->unsignedBigInteger('packaging_id'); 
 $table->foreign('packaging_id')->references('id')->on('packagings');  
});          

In my ProductPackaging model I have:

  public function packaging()
  {
    return $this->hasOne(Packaging::class)
  }

And in my ProductPackaging resource I have

public function fields(NovaRequest $request)
{
  return [
     ID::make()->sortable(),
     HasOne::make('Packaging'),
  ];
}

Now when I try to create a ProductPackaging I get this error:

 Column not found: 1054 Unknown column 'packagings.product_packaging_id' in 'where clause' (SQL: select * from `packagings` where `packagings`.`product_packaging_id` in (0))

And I don't understand why I should have a product_packaging_id in my packagings table. I must be doing something wrong. But don't understand what.

0 likes
8 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

This is belongs to

public function packaging()
  {
    return $this->belongsTo(Packaging::class)
  } 
1 like
sandersjj's avatar

@Sinnbeck Thanks for your quick reply. I tried this. It gives me this next error: Method Illuminate\Database\Eloquent\Collection::getKey does not exist. Triggered by: vendor/laravel/nova/src/Fields/BelongsTo.php

Also I don't really understand your solutions since we are dealing with a hasOne relationship.

Sinnbeck's avatar

@jigalroecha@gmail.com why is it has one? If Product Packaging has a column that points to Packaging id, then it belongs to it (think of it as child to parent)

sandersjj's avatar

@Sinnbeck One packaging can belong to many ProductPackagings. And one Packaging has one ProductPackaging.

sandersjj's avatar

@Sinnbeck Ah got it. Many Thanks. so where does this come from? Illuminate\Database\Eloquent\Collection::getKey does not exist. ?

Sinnbeck's avatar

@jigalroecha@gmail.com sounds like you have set up a hasMany relationship and try to use it as if there is only one

sandersjj's avatar

@Sinnbeck I have solved it. In my ProductPackaging resource I still had hasOne in stead of BelongsTo

Please or to participate in this conversation.