edstevo/laravel-dao
Laravel DAO layer for models, inspired by repository pattern. Adds caching, broadcast events, validation, and model/DAO generators to centralize data access and keep controllers flexible across Laravel apps.
Installation:
composer require edstevo/laravel-dao
Publish the config file:
php artisan vendor:publish --provider="Edstevo\LaravelDao\LaravelDaoServiceProvider"
Configure:
Edit config/laravel-dao.php to set default cache driver (e.g., file, redis) and event broadcasting preferences.
First Use Case:
Generate a DAO for an existing model (e.g., User):
php artisan dao:generate User
This creates a UserDao class in app/Dao/UserDao.php with default methods (find(), all(), etc.) and a corresponding UserDaoServiceProvider.
config/laravel-dao.php (cache, events, validation rules).app/Dao/ (customize these for your logic).app/Providers/DaoServiceProvider.php (register DAOs globally).DAO Generation:
Use php artisan dao:generate ModelName to scaffold a DAO with:
find(), create(), update(), delete()).find() checks cache before querying DB).created, updated events).Customizing DAOs: Extend the generated DAO to add business logic:
// app/Dao/UserDao.php
class UserDao extends \Edstevo\LaravelDao\Dao
{
public function findActiveUsers()
{
return $this->model->where('active', 1)->get();
}
}
Cache Integration: Leverage the built-in cache layer for read-heavy operations:
// Automatically cached for 10 minutes
$user = $this->userDao->find($id, 10);
Validation:
Define rules in the DAO’s rules() method:
protected function rules()
{
return [
'email' => 'required|email|unique:users',
'password' => 'required|min:8',
];
}
Event Broadcasting:
Events are auto-broadcasted for create, update, and delete operations. Listen via:
// In an EventServiceProvider
protected $listen = [
'user.created' => [
'App\Listeners\SendWelcomeEmail',
],
];
Dependency Injection:
Bind DAOs in app/Providers/DaoServiceProvider.php:
$this->app->bind('App\Dao\UserDao', function ($app) {
return new \App\Dao\UserDao(new \App\User);
});
Inject DAOs into controllers/services:
public function __construct(UserDao $userDao) {
$this->userDao = $userDao;
}
Model Generators:
Use php artisan dao:generate ModelName to auto-create DAOs for new models. Customize the stubs in vendor/edstevo/laravel-dao/src/stubs/.
Testing: Mock DAOs in tests to isolate logic:
$this->mock(UserDao::class)->shouldReceive('find')->andReturn($user);
Outdated Package:
laravel/framework, illuminate/cache).Cache Key Conflicts:
model_name:id. Override in DAO:
protected function getCacheKey($id)
{
return "custom_prefix:{$id}";
}
Event Broadcasting:
QUEUE_CONNECTION is configured in .env.sync).Validation Overrides:
rules() apply to create and update. For custom methods, manually validate:
$validator = Validator::make($data, $this->customRules());
Model Binding:
UserDao → User). Override getModel() if using a different naming convention:
protected function getModel()
{
return new \App\Models\CustomUser();
}
Cache Issues:
php artisan cache:clear
storage/framework/cache.Event Debugging:
php artisan tinker
Event::listen('user.created', function ($user) {
echo "User created: {$user->id}\n";
});
DAO Method Overrides:
protected or public explicitly.Custom Cache Drivers:
Extend the cache layer by overriding getCache() in your DAO:
protected function getCache()
{
return Cache::store('redis')->rememberForever(...);
}
Dynamic Rules: Load validation rules from a separate file:
protected function rules()
{
return require __DIR__ . '/validation_rules.php';
}
Observer Integration: Use DAO events to trigger observers:
// In UserObserver
public function created(User $user)
{
event(new UserCreated($user));
}
API Responses: Wrap DAO responses in API resources:
public function find($id)
{
$user = parent::find($id);
return new UserResource($user);
}
How can I help you explore Laravel packages today?