A wrapper around eloquent's main features so you can share them among your own repositories
In order to install this package, all you have to do is execute the following line in your console
composer require inquiloper/laravel-baserepository
Then in your repositories
use Inquiloper\BaseRepository;
use App\User;
class UsersRepository extends from BaseRepository implements UsersRepositoryInterface {
public function __construct(User $user){
parent::__construct($user);
}
}
That's it, you're ready to rock now!
Let's suppose you have this function in your controller where you type-hint your repo
public function users(UsersRepositoryInterface $usersRepo){}
Now, the methods you have available are:
$usersRepo->findAll()
$usersRepo->findOneBy(Array $fields)
$usersRepo->findAllBy(Array $fields)
$usersRepo->create(Array $data)
$usersRepo->updateBy(Array $fields, Array $data)
public function with($relationships);
$usersRepo->updateBy(Array $fields, Array $data)
$usersRepo->deleteBy(Array $fields)
$usersRepo->with(['posts', 'comments'])->findOneBy(['id' => 1])
You can call the findOneBy method with a dynamic name like so:
$usersRepo->findOneByName('John Doe')
This will translate to select * from users where name = 'John Doe' limit 1
The dynamic calling will automatically snake_case anything after findOneBy , so Name converts to name, UserEmail to user_email and so on. You can turn this off and the method will add the where clause with the literal name
$usersRepo->findOneByWpEmail('email@example.com' , false)
This will translate to select * from users where WpEmail = 'email@example.com' limit 1