Search code examples
phplaraveleloquentstatic-methodslumen

How can I get table name, statically from Eloquent model?


Right now I have this code to check to which table an Eloquent model is connected into.

$s = new Something();
dd($s->getTable());

Is there anyway I can get the table without instantiating new Something object?

I was thinking something like these codes:

Something::getTable();

But there will be ..should not be called statically error.


Solution

  • You can add to your model.

    public static function getTableName()
    {
        return (new self())->getTable();
    }
    

    Then you can get table name with Something::getTableName()