Reputation:
I use the repository pattern in lumen together with Query Builder. The repository classes look generally like this:
<?php
namespace App\Repositories;
use Illuminate\Support\Facades\DB;
class RepoNameRepository {
public function methodName() {
/*
Various Calls to the DB facade...
$data = DB::table("tableName")...->get();
*/
return $data;
}
}
Is there any efficient way to get rid of the use statement use Illuminate\Support\Facades\DB at the beginning of each of my repository classes? Ideally, the DB facade would just be available as is in web.php.
What I could think of so far is to have a Repository base class with the use statement.
Upvotes: 0
Views: 1192
Reputation: 9161
You can use the app()
helper function with the 'db'
service container binding key:
$data = app('db')->table('tableName')...->get();
It doesn't require any use
statement.
Upvotes: 1
Reputation: 12460
No, this is how PHP works.
Your alternatives are to use the alias:
use DB;
Or prefix your calls with a backslash so it looks in the root namespace.
\DB::table('users');
Upvotes: 1