Saturday, November 30, 2019

Laravel 5.5 new feature - Route::view() and Route::redirect() method with example

In this tutorial, I will tell you the new features Route::view() and Route::redirect() helpers added with Laravel 5.5.
Route::view()
Sometime you need to return only a view then you define a route and then return view using view() helpers function.
With the release of Laravel 5.5, You can use the Route::view method that provides a easier shortcut load view for a URI.
Route::view method accepts a URI as a first argument and in second argument you will pass the view name.
Before Laravel 5.5
Before Laravel 5.5, If you need to return view only for specific URI then you were using the following syntax :
  1. Route::get('/',function(){
  2.     return view('dashboard');
  3. });
With Laravel 5.5
Laravel 5.5 provides the shortcut for achieving the above functionality in single line:
Route::view('/','dashboard');
Route::redirect()
Route::redirect method provides the shortcut to redirect from one URI to another URI.
Before Laravel 5.5
Sometime you want to redirect to a specific URI by accessing another URI then you defined the route with particular URI and then redirect to specific URI using the redirect method in following way :
  1. Route::get('/',function(){
  2.     return redirect('/dashboard');
  3. });
In the above example, If a user hits the root url then he will be redirected to the dashboard.
With Laravel 5.5
You can achieve the same functionality by using Route::redirect() method in following way :
Route::redirect('/','dashboard');
Continue reading...

Laravel 5.5 new feature - Custom Validation Rules with example

In this tutorial, I will tell you the new features for adding custom validation rules in Laravel 5.5.
In Laravel 5.5, You can define a dedicated class to handle the validation.
In the previous Laravel version, There is still possibility to define custom validation using Validator::extend method but custom validation rule object is an excellent alternative solution.
You can generate a class for implementing custom validation rules in Laravel 5.5 by running new artisan command :
php artisan make:rule CustomValidationRule
After running above command, You will get a file CustomValidationRule.php in following path app/Rules/.
Class CustomValidationRule will have two methods passes and message
The passes method will have two method $attribute and $value that can be used to validate the field and message method return the respected error message when validation fails.
Let's have a example for age validation :
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. class AgeValidationRule implements Rule
  5. {
  6. public function passes($attribute, $value)
  7. {
  8. return $value > 18;
  9. }
  10. public function message()
  11. {
  12. return ':attribute should be greater than 18!';
  13. }
  14. }
Now in the controller, you can define the custom validation rules with request in following way :
  1. public function store()
  2. {
  3. // Validation message would be "age should be greater than 18!"
  4. $this->validate(request(), [
  5. 'age' => [new AgeValidationRule]
  6. ]);
  7. }
You can also use the constructor to inject any further dependency if you want.
If you use custom validation rule then you can not pass a string with rules seperated by a comma. You will have to pass each rule as a single element in an array like this :
  1. public function store()
  2. {
  3. // Validation message would be "age should be greater than 18!"
  4. $this->validate(request(), [
  5. 'age' => ['required',new AgeValidationRule]
  6. ]);
  7. }
Click here to see the custom validation rules in previous Laravel version :
Continue reading...

Friday, November 29, 2019

Laravel Tinker with PHP Artisan command to update user details

In this Laravel tutorial, I will tell you about the 'Tinker' one of the awesome features in Laravel application which allow user to interact with entire Laravel applications from the command line.
You can put all eloquent queries on command line with the help of tinker.
With the help of Laravel's lesser-known features, You can quickly read data from the Database in Laravel application.
Laravel tinker is a repl (Read–Eval–Print Loop) powered by the PsySH package.
Before tinker, install the laravel application and the run the migration command to create table :
php artisan migrate
After running migration command, you will see the following output :
Now run artisan command to enter into tinker environment :
php artisan tinker
Seeding Database with Test Users
First, we will seed our database with 10 new user details by running following line of command :
factory(App\User::class, 10)->create();
ou can count the total number of users in the database by running following command :
App\User::count();
Adding a New User
You can create a user from the repl. I have already told you that you can put your eloquent queries just like you write code in Laravel application :
  1. $user = new App\User;
  2. $user->name = "Ajay";
  3. $user->email = "ajay.agrahari09@gmail.com";
  4. $user->password=bcrypt('123456');
  5. $user->save();
Output :
Update User Details
Run the query to update user details :
  1. $user = App\User::find(2);
  2. $user->name='Test User';
  3. $user->save();
Output :
Delete User
Run the following query to delete user from database :
  1. $user = App\User::find(1);
  2. $user->delete();
Output :


Continue reading...