How to call table name in model Laravel

Laravel works on the Model View Controller (MVC) structure. We create a model for every table in the Laravel application. The database logic goes in a Model. To call table name in model Laravel you can use the getTable() function.

We can manipulate database table data in many ways using Laravel Eloquent Model. There are several methods like all(), first(), and get() that can be used to call a table and get table from the table model.

Models in Laravel comes under the app/Models directory. You can create a new Model using the command below.

php artisan make:model User

To create a database migration for your newly created model, you can pass the command below.

php artisan make:model User --migration

An Eloquent Model will correspond to a database table to interact with the table. These models can also help retrieve records from the table and perform multiple operations. You can perform multiple operations like insert, edit, or delete records in the database tables from a model. However, you should first get the table name using the getTable() function, and then perform relevant operations.

Let’s look over the syntax on how we can call table name using the getTable() method in a Laravel mode.

protected $table = 'UserSettings';

You will call the table name in a Laravel model.

protected $table = 'user_table';

Read the table name from the database model using the getTable() method.

$item = new Item;
$table = $item->getTable();
print_r($table);