lunar icon indicating copy to clipboard operation
lunar copied to clipboard

Table prefix duplicated when extending a Lunar model multiple times via HasModelExtending trait

Open somegooser opened this issue 3 months ago • 2 comments

  • Lunar version: 1.1.0
  • Laravel Version: 12
  • PHP Version: 8.4
  • Database Driver & Version: sqlite

Bug: Table prefix duplicated when extending a model multiple times

Lunar’s table prefixing works for single inheritance, but breaks when a model extends another extended model.

Working example

App\Models\Product extends Lunar\Models\Collection;

// getTable() => shop_products

Failing example

App\Models\Custom\Product extends App\Models\Product;

// getTable() => shop_shop_products  // prefix applied twice

Cause

HasModelExtending::getTable() only checks one parent class:

public function getTable()
{
    $parentClass = get_parent_class($this);

    return $parentClass == BaseModel::class
        ? parent::getTable()
        : (new $parentClass)->table;
}

This returns the already-prefixed table name from the parent, causing the prefix to be applied multiple times.

Expected

Prefix should be applied once, based on the base model’s table name, regardless of inheritance depth.

somegooser avatar Nov 19 '25 13:11 somegooser

We don't support multiple extending. But if it's important to you, we'd welcome a PR.

glennjacobs avatar Nov 19 '25 14:11 glennjacobs

I just did

#2353

somegooser avatar Nov 20 '25 08:11 somegooser