Laravel get table name from model example

In this article explained of laravel get table name from model. If you need to get table name from model in your laravel application then this article post can help you. So, we require to create model for every table that method we can write database logic on model. But if you require to get table name from model then you can use laravel method getTable() in your application.

Laravel eloquent model provide several method like all(), get(), first() etc, in this method helps to get table record from model table.

If you are required to get the table name simply call getTable() of model object, so how to call this method as below example. 

Laravel Get table name from model using getTable() method
Example
<?php
use App\Models\Test;

$test = new Test();
$tableName = $test->getTable();

echo $tableName; // Output: 'tests'

?>
Dynamic table name retrieval

If you have an instance of the model, you can also retrieve the table name dynamically.

<?php
use App\Models\Test;

$testInstance = Test::find(1);
$tableName = $testInstance->getTable();

echo $tableName; // Output: 'tests'

?>

I hope this tutorial help you.

Leave a Reply

Your email address will not be published. Required fields are marked *