15

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.

kjones
  • 1,339
  • 1
  • 13
  • 28
notalentgeek
  • 4,939
  • 11
  • 34
  • 53
  • You can take a look here : http://php.net/manual/fr/language.oop5.static.php – Inazo Aug 23 '18 at 07:34
  • 1
    `(new static)->getTable()` – Brian Lee Aug 23 '18 at 07:38
  • @Inazo This has nothing to do with just the (French?) documentation of static's in PHP. – Douwe de Haan Aug 23 '18 at 07:38
  • Problem is that the `$table` variable that you could use to specify a custom table name for the model is not static so you can't access it statically. It's weird but also by design. (Read [eloquent model conventions](https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions) under table names) – apokryfos Aug 23 '18 at 07:45
  • I wrote an answer for [this question](https://stackoverflow.com/a/62654389/6212294) that answers your question too. Also, you can get table name by calling a **static function** in this way. There is no need to make a object. – SEYED BABAK ASHRAFI Jun 30 '20 at 09:35
  • Does this answer your question? [How to return database table name in Laravel](https://stackoverflow.com/questions/14082682/how-to-return-database-table-name-in-laravel) – SEYED BABAK ASHRAFI Aug 04 '22 at 16:15

7 Answers7

27

You can add to your model.

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

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

Sang Nguyen
  • 1,702
  • 15
  • 16
  • `get_called_class()` is helpful if you want to define `getTableName()` in a parent model that the actual models extend. https://stackoverflow.com/a/283094/470749 – Ryan Feb 08 '20 at 00:16
  • 5
    @Ryan Or `return (new static)->getTable()` in base model. – М.Б. Feb 11 '21 at 21:37
14

Here's a slight modification so that you can get a model's table name statically.

  1. Define a Trait: app/Traits/CanGetTableNameStatically.php
<?php namespace App\Traits;

trait CanGetTableNameStatically
{
    public static function tableName()
    {
        return with(new static)->getTable();
    }
}
  1. Put in on your Model, or better yet, use a BaseModel and all your other models extends it.

app/Models/BaseModel.php

<?php namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\CanGetTableNameStatically;

class BaseModel extends Model
{
    use CanGetTableNameStatically;

    // ...
}
  1. On your other models, you can set the custom table name on Laravel's reserved attribute: protected $table

app/Models/Customer.php

<?php namespace App\Models\Master;

use App\Models\BaseModel;

class Customer extends BaseModel
{
    protected $table = 'my_customers';

    // ...
}

Usage: just call YourModel::tableName() anywhere.

In Views:

{{ \App\Models\Customer::tableName() }}

When doing Joins:

DB::table( Product::tableName() . ' AS p' )
->leftJoin( ProductCategory::tableName() . ' AS pc', 'pc.id', '=', 'p.category_id')
// ... etc
kjones
  • 1,339
  • 1
  • 13
  • 28
topher
  • 1,357
  • 4
  • 17
  • 36
5

Not static but elegant approach

with(new Something)->getTable();
nyahs
  • 61
  • 1
  • 2
1

Resurrecting this, but it must be said. Similar problem, ended up doing this to models.

    public const TABLE_NAME = 'table_name';
    protected $table = self::TABLE_NAME;
aconrad
  • 556
  • 5
  • 12
0

I think is is a simplest way to do get the model's table name in laravel

$table_name = (new ModelName())->getTable(); 
General Grievance
  • 4,555
  • 31
  • 31
  • 45
MUHINDO
  • 788
  • 6
  • 10
0

You can try to write macros to do that.

Write the following macro in the boot method of app/Providers/AppServiceProvider.php

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

EloquentBuilder::macro('tableName', function (): string {
    assert($this instanceof EloquentBuilder);
    return $this->from;
});

Now simply call User::tableName() to get the table name of the model.

Daniel Sun
  • 50
  • 5
-1

$model = new Model; echo $model->getTable(); Try this