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:
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:
Exception Handling in Laravel
Error and exception handling is already configured in laravel project. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. To send an email when an exception comes write the following codes in your App\Exceptions\Handler class in the report():
public function report(Exception $exception)
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...
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...
Artisan Commands in Laravel
Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application. To view a list of all available Artisan commands, you may use the list command:
php artisan list
Every command also includes a "help" screen which displays and describes the command's available arguments and options. To vi...
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:
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);
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.