Skip to content

Instantly share code, notes, and snippets.

@pthiers
Last active July 31, 2017 14:12
Show Gist options
  • Save pthiers/bae5ce0fb47b265136311f63a88950b6 to your computer and use it in GitHub Desktop.
Save pthiers/bae5ce0fb47b265136311f63a88950b6 to your computer and use it in GitHub Desktop.
'static' way for getting the table name in eloquent, Laravel > 5.2 .
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as BaseModel;
abstract class Model extends BaseModel
{
protected static $tableName = [];
public static function table(){
$className = get_called_class();
if(!isset(self::$tableName[$className])){
self::$tableName[$className] = (new $className)->getTable();
}
return self::$tableName[$className];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment