Last active
July 31, 2017 14:12
-
-
Save pthiers/bae5ce0fb47b265136311f63a88950b6 to your computer and use it in GitHub Desktop.
'static' way for getting the table name in eloquent, Laravel > 5.2 .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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