Learn about my progress!
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:
Next, register the&nb...
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 ...
Resourceful Controllers in Laravel
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the make:controller Artisan command, we can quickly create such a controller:
php artisan make:controller PhotoController...
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:
Accessors and Mutators in Laravel
Defining An Accessor
To define an accessor, create a getFooAttribute method on your model where Foo is the "studly" cased name of the column you wish to access. In this example, we'll define an accessor for the first_name attribute. The accessor will automatically be called by Eloquent when attempting to retrieve the value of the first_name attribute:
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 ...
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 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.
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);
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.']);