Laravel morph relationship

Laravel morph relationship

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding Laravel Morph Relationships

When working with polymorphic relationships in Laravel, the temptation to treat them like standard one-to-many associations often leads to subtle but painful bugs. A common scenario involves a single collection of records (like products) that can reference different types of underlying data (like Event, Item, or Service). The database schema for this usually involves a pivot table containing both the ID of the parent and a foreign key to the child, along with a polymorphic column.

Consider a system where a Shop has many Products. Each product represents an entity that could be an Event, an Item, or a Service. The database structure typically looks like this:

  • shops: Contains standard shop data including id and user_id.
  • events: Stores specific details like title, description, and whether it is public.
  • products: Acts as the pivot table. It holds shop_id to link back to the shop, an id for the product record itself, and two columns—productable_id and productable_type—to store the reference to the specific polymorphic model.

The challenge arises when trying to assign a new polymorphic instance directly to the relationship using save(). If you attempt the following logic:

$shop = Shop::first();
$event = Event::create(['title' => 'Some event']);
$service = Service::create(['title' => 'Some service']);

// This is where things break
$shop->products()->save($event); 
$shop->products()->save($service);

You might encounter an error similar to SQLSTATE[HY000]: General error: 1 no such column: shop_id. The SQL query generated attempts to update the target model (e.g., accendo_events) with a shop_id value. This indicates that Laravel is treating the relationship as a standard many-to-many or a direct foreign key join where it expects a unique foreign key on the child table, rather than updating the polymorphic columns on the pivot table.

The root cause lies in how the relationship methods are defined and invoked. The error occurs because save() on a collection assumes that the relationship is defined via a standard hasMany method on the parent or a morphTo/belongsToMany setup that doesn't explicitly handle the polymorphic assignment via the pivot table's specific columns in this context.

The correct approach requires defining the relationships explicitly and ensuring the models know how to map the polymorphic data. On the Shop model, you should define a standard hasMany relationship pointing to the Product model:

class Shop extends Model
{
    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

On the Product model, which acts as the pivot or accessor for the polymorphic data, you define a morphTo relationship. This tells Laravel that this table (products) is referencing various models:

class Product extends Model
{
    public function productable()
    {
        return $this->morphTo();
    }
}

Conversely, on the specific child models like Event or Service, you must define a morphOne relationship pointing back to the Product model. Crucially, the second argument of morphOne should specify the column name used for the foreign key and type:

class Event extends Model
{
    public function product()
    {
        return $this->morphOne(Product::class, 'productable');
    }
}

However, simply saving an instance through $shop->products()->save($event) does not automatically handle the polymorphic assignment logic required for this specific schema pattern. The save() method on a collection expects the child model to have a foreign key relationship defined directly to the parent's primary key (e.g., shop_id), which doesn't exist on the Event table.

To achieve the desired functionality, you must bypass the automatic collection save logic and manually persist the polymorphic record. The most robust way to do this is to create a new instance of the Product model, assign the polymorphic data to it, and then attach that product to the shop. This ensures the shop_id, productable_id, and productable_type are populated correctly in the products table.

$event = Event::create(['title' => 'Some event']);
$product = new Product();
$product->productable()->associate($event);
$product->shop()->associate($shop); // Or set shop_id directly based on schema preference
$product->save();

Alternatively, if you are using Laravel's newer Eloquent features or a specific package pattern, you can leverage the attach method on the relationship, but this requires the child model (Product) to be instantiated first. The key takeaway is that polymorphic relationships do not support direct assignment of the polymorphic instance via the parent's collection save method unless the foreign key structure supports it directly. You must explicitly manage the pivot table data by associating the Product instance with both the Shop and the specific Event/Service before saving.

For more deep dives into Eloquent relationship nuances, resources like LaravelCompany often provide additional context on advanced pattern implementation.

Our network