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...

Stripe Integration in Laravel

Stripe is a US technology company, operating in over 25 countries, that allows both private individuals and businesses to accept payments over the Internet. For integrating Stripe in Laravel, first add the Cashier package for Stripe to your composer.json file and run the composer update command: "laravel/cashier": "~7.0" Next, register the&nb...

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; ...

Facades in Laravel

Facades provide a static interface to classes that are available in the application service container. Laravel ships with many facades which provide access to almost all of Laravel's features. Laravel facades serve as static proxies to underlying classes in the service container, providing the benefit of a terse, expressive syntax while maintaining more testability and flexibility th...

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 ...

Logging Request and Response with Controller and Function Name

Logging is one of the most powerful tools in software development. It gives you an idea of what your code is doing. In Laravel handling logs is very easy. When you start a new Laravel project, error and exception handling is already configured for you. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. I have ...

Inserting and Updating Related Models in Laravel

The Save Method Eloquent provides convenient methods for adding new models to relationships. For example, perhaps you need to insert a new Comment for a Post model. Instead of manually setting the post_id attribute on the Comment, you may insert the Comment directly from the relationship's save(): $comment = new App\Comment(['message' => 'A new comment.']); $post ...

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...

CSRF Protection in Laravel

Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user. Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that ...

Many To Many Relationships Laravel

Attaching / Detaching Eloquent also provides a few additional helper methods to make working with related models more convenient. For example, let's imagine a user can have many roles and a role can have many users. To attach a role to a user by inserting a record in the intermediate table that joins the models, use the attach() method: $user = App\User::find(1); $user->roles()-&...