Use Laravel query builder without hard-coding the table names

Problem:

$users = \DB::table('users')->get();
$users = \DB::table('members')->get();

Solution:

$users = \DB::table((new \App\Models\User)->getTable())->get();
$users = \DB::table(app()->make(\App\Models\User::class)->getTable())->get();
// in the app/models/User.php fileprotected $table = 'members';

Backend developer