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 I've been unsuccessful calling it calling it from the model that I'm in.
You can get table name simply call getTable() of model object, so how to call this method as bellow example. $item = new Item; $table = $item->getTable(); print_r($table);
You can get all the column names from a table using the DB facade and Schema facade. You have to call the getColumnListing() method (using DB and Schema facade) by passing the table name as an argument to get all columns from the table.
There is a public getTable() method defined in Eloquent\Model so you should be able to use $model->getTable()
.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With