### Implement Model Hooks in Doppar (PHP) Source: https://doppar.com/docs/index Provides an example of implementing Model Hooks in Doppar to execute custom logic during model lifecycle events like creation or updates. It includes a hook handler and a conditional execution example. ```php [ 'handler' => App\Hooks\UserUpdatedHook::class, 'when' => [self::class, 'isAdmin'] ], ]; public static function isAdmin(Model $model): bool { return (int) $model->role === 'admin'; } } class UserUpdatedHook { public function handle(Model $model): void { if ($model->isDirtyAttr('name')) { info("Name changed from {$model->getOriginal('name')} to {$model->name}"); } } } ``` -------------------------------- ### Interact with Language Models using Doppar AI Agents (PHP) Source: https://doppar.com/docs/index Demonstrates how to use Doppar AI Agents to interact with language models, supporting both self-hosted and OpenAI deployments. Requires the Doppar AI library. ```php withHost('http://localhost:1234') ->model('local-model-name') ->prompt('Generate a PHP function to validate email') ->send(); // With OpenAI use Doppar\AI\Agent; use Doppar\AI\AgentFactory\Agent\OpenAI; $response = Agent::using(OpenAI::class) ->withKey(env('OPENAI_API_KEY')) ->model('gpt-3.5-turbo') ->prompt('Explain Doppar PHP Framework') ->send(); ``` -------------------------------- ### Define Routes with Doppar Routing Attributes (PHP) Source: https://doppar.com/docs/index Illustrates how to define routes in the Doppar framework using attributes for URIs, HTTP methods, naming, middleware, and rate limiting. Supports route-model binding. ```php whereStatus(true); } Post::active() ->present('comments.reply', function ($query) { $query->where('approved', true); }) ->search( attributes: [ 'title', 'user.name', 'category.name', 'tags.name', 'comments.body', 'comments.reply.body', ], searchTerm: $request->search ) ->embed( relations: [ 'category:id,name', 'user:id,name', 'tags', ] ) ->embedCount([ 'tags', 'comments.reply' => fn($q) => $q->where('approved', true), ]) ->paginate(perPage: 10); ``` -------------------------------- ### Parameter-Level Hydration with #[BindPayload] Source: https://doppar.com/docs/index Demonstrates how to use the #[BindPayload] attribute in PHP to automatically hydrate a model object from a request payload, reducing boilerplate code compared to traditional manual validation and data extraction. ```php public function store(Request $request) { // 1. Validate the request data. // 2. Extract the data. // 3. Create a new User object and fill it manually. $user = new User(); $user->name = $request->input('name'); // ... many more lines of mapping $user->save(); } // With #[BindPayload] Attribute public function store( #[BindPayload] User $user // The $user object is fully ready! ) { return User::createFromModel($user); } ``` -------------------------------- ### Automatic Transaction Wrapping with Doppar Attribute (PHP) Source: https://doppar.com/docs/index Demonstrates how to automatically wrap methods in a database transaction using the `#[Transaction]` attribute in Doppar, eliminating boilerplate code. ```php