Latest Blogs

Learn about my progress!

Using Events on Laravel Eloquent

Eloquent models fire several events, allowing you to hook into the following points in a model's lifecycle: creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored. Events allow you to easily execute code each time a specific model class is saved or updated in the database. Whenever a new model is saved for the first time, the creating, created events wil...

Eager Loading in Laravel

When accessing Eloquent relationships as properties, the relationship data is "lazy loaded". This means the relationship data is not actually loaded until you first access the property. However, Eloquent can "eager load" relationships at the time you query the parent model. Eager loading alleviates the N + 1 query problem. To illustrate the N + 1 query problem, consider a Book ...

Password Broker in Laravel

Password Broker: Password Broker in laravel is used to reset forgotten password for users. PasswordBroker class will create a token for user and send an email for the set new password link, where user can set a new password. To reset password make PasswordsController and a form which will submit your email. It will point to your store() in PasswordsController which will have code...

Making Image Uploading Trait in Laravel

For uploading image in laravel we can simply make a trait and use it in any model and do the image upload. We can also do validations on the image which is explained in http://blog.samarthasinha.com/blog/making-validation-trait-laravel and http://blog.samarthasinha.com/blog/using-events-laravel-eloquent link. The code for image validation is below: <?php namespace App\Traits; ...

Making Validation Trait in Laravel

In PHP we use Traits for code reusibility. If we have to make same functions in different classes we can simply make a trait and use it in different classes. In Laravel Validation is an important part which we have to make in models and we have to do validations in objects. So we can make a ValidationTrait.php file in app\Traits folder and write the following codes. <?php namespace...