How to return database table name in Laravel

前端 未结 11 540
执念已碎
执念已碎 2020-12-28 13:28

Is there a way that I can get the current database table in use by the model that I\'m in? I see that there is a table() function in Laravel/Database/Eloquent/model.php but

相关标签:
11条回答
  • 2020-12-28 13:36

    I just wanted to add the following for people coming from search engines:

    In case you do not even want to instantiate the Model at all (faster?) :

    $model = 'App\User';
    $modelTable = str_replace('\\', '', Str::snake(Str::plural(class_basename($model))));
    dd($modelTable); // will return "users"
    

    That might look ugly but that's exactly how the getTable() method resolves it under the hood, so...

    You will need to use Illuminate\Support\Str; on top of your file.

    Addendum: implying you follow the framework's standards (i.e: Post model has posts table, User model has users table, etc)

    0 讨论(0)
  • 2020-12-28 13:44

    Since table is a protected property in the Model class (Laravel >= 5) you will need an instance of your Model.

    Here is a case example:

            DB::table( (new YourModelClassname)->getTable() )
                ->update(['field' => false]);
    
    0 讨论(0)
  • 2020-12-28 13:44

    Based on Lucky Soni answer, there is another easy trick if you want to directly call it from Vontroller or View.

    Tested in Laravel 6, and I keep using it, if you are "One Line Programmer" who hates extra line instance declaration. No need for extra lines in Model file too.

    $string_table_name = with(new \App\Model\TableModelName)->getTable();
    

    or better you may also be able to just call this

    $string_table_name = (new \App\Model\TableModelName)->getTable();
    

    It will return plain string of the tabel name even if you rename $table variable inside model class.

    EDIT :

    Minus Rep ?? Maybe you should try this first in your controller instead making new function in model class just to get table name and no need to declare the object when calling.

    with() itself is Laravel helper function that returns an object of the class. and inside class that extends Model, already has function getTable(). So, you don't have to put another new redundant function inside model class. It seems the latest version, you can just call (new Class) without with() function.

    The difference between this answer and Lucky's answer, mine doesn't make any new function inside Model class to get the table name, even you can just call the function inside the Controller and View without declaring the object of model class. It's for beautify the code.

    While Lucky's answer create new function that inside Model class, and you need to call the function from the object.

    0 讨论(0)
  • 2020-12-28 13:46

    Taylor has an answer to your question:

    Within the model class you can do something like this:

    return with(new static)->getTable();
    

    If you want all your models to have the ability to return table name statically, then so something like this:

    class BaseModel extends Eloquent {
    
        public static function getTableName()
        {
            return with(new static)->getTable();
        }
    
    }
    
    class User extends BaseModel {
    
    }
    
    
    User::getTableName();
    
    0 讨论(0)
  • 2020-12-28 13:52

    In my case, i'm using laravel 5.4

    return (new static)->getTable();

    0 讨论(0)
提交回复
热议问题