Wednesday, 16 August 2017

New Features in Laravel 5.5 Your Next Project


New Features in Laravel 5.5:

1. Frontend Presets
2. DD and Dump for Easy Debugging
3. Custom Email Theme Setup
4. Custom Validation Rule
5. Laravel 5.5 Require PHP 7+
6. Laravel Auto Package Discovery

1. Frontend Presets


As we all know Laravel 5.4 have vue.js as preset front end JavaScript but Laravel 5.5 will be introduced with 3 front ends preset: Vue.js as default, react.js and bootstrap, you can change as per your preference.

you can change the preset to bootstrap using this command:


php artisan preset bootstrap



We can change the preset to react and vue by replacing the bootstrap in above command, Laravel 5.5 will also provide the option to remove the all 3 to load your own JavaScript or PHP backend only app by the following command:

php artisan preset none

2. DD and Dump for Easy Debugging 

 In collection class of Laravel 5.5, two methods will be introducing for easy debugging-DD and Dump, let’s understand by simple example:

collect([1,2,3])->map(function($i){
   return $i * 3;
})->reject(function($i){
   return $i < 3;
});


In above condition knowing the all step of each chain may be difficult so you can dump at some point.

collect([1,2,3])->map(function($i){
   return $i * 3;
})->dump()->reject(function($i){
   return $i < 3;
});


3. Custom Email Theme Setup

Today’s new era of software industry brand expression is most important in every process of the product while a user interacts with it, In an earlier version of laravel we can easily send the email but has been sent using default email template. If you want to use your branding in Laravel 5.4 then you need to follow below steps:

You can start by generating a CSS file containing your custom styling like below:
// my-design.css is the name of your theme
resources/views/vendor/mail/html/themes/my-design.css


Now update the markdown option of your mail.php config file:

'markdown' => [
  'theme' => 'my-design',
  'paths' => [
    resource_path('views/vendor/mail'),
   ],
],


In Laravel 5.5, we can send the custom email template with your perfect branding more easily. i.e you wanted to send the notification email to users then you can write your own CSS.

//Create css style sheet
resources/views/vendor/mail/html/themes/techuzITFirm.css


Now define $theme property that references your new theme on any Mailable class

class SendInvoice extends Mailable

{
protected $theme = 'techuzITFirm';
...
}


4. Custom Validation Rule

Laravel 5.5 will introduce the dedicated class to handle the validation, to define the custom validation rule you need to create the class with two methods: Passes and message, let’s take an example to understand better:

<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class HireProfessionalLaravelDeveloperIndia implements Rule
{
  public function passes($attribute, $value)
  {
   return $value > 5;
  }
  public function message()
{
    return ':attribute needs more Developers!';
  }
}

5. Laravel 5.5 Require PHP 7+

Laravel 5.5 will have PHP 7+ version, it will improve the speed of web application and will save a cost for your project. You can check the report of engineering team of tumblr who have recently implemented the PHP 7+ version
“Almost immediately saw the latency drop by half, and the CPU load on the servers decrease at least 50%, often more. Not only were our servers serving pages twice as fast, they were doing it using half the amount of CPU resources – Tumbler engineering report“
“Etsy is a popular PHP-based online marketplace for handmade and vintage goods that has been in operation since 2005 (29 million items listed on its website, 54 million registered users, among them 1.4 million active sellers, and 19.8 million active buyers). Initially it was written in PHP 5, and later, was switched to HHVM (Virtual Machine for PHP). After migration to PHP 7, Etsy was significantly better from such perspectives as the memory and system/user CPU usage, homepage/listing delivery speed and number of hosting servers – Etsy Case Study”

6. Laravel Auto Package Discovery


In earlier version developer had to write the following package step to register its service providers and adding aliases before pulling, Laravel 5.5 have new cool way for package developer, you can review the Taylora’s post for more detail.
Install the package:

composer require school/kids

School/Kids\ServiceProvider::class,
‘Bar’ => School/Kids\Facade::class,


New cool way to write the pull request automatically register service providers and add aliases through the package’s composer.json in Laravel 5.5

"extra": {
   "laravel": {
    "providers": [
     "School\\Kids\\ServiceProvider"
   ],
    "aliases": {
    "Bar": "School\\Kids\\Facade"
    }
   }
}


Register the provider in app.php:
Optionally, register Facade:
 

Monday, 14 August 2017

How to install Laravel 5.5 project from scratch


Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

Via Laravel Installer

composer global require "laravel/installer"

Once installed, the laravel new command will create a fresh Laravel installation in the 
directory you specify. For instance, laravel new blog will create a directory named blog 
containing a fresh Laravel installation with all of Laravel's dependencies already installed:

laravel new Laraquery

Via Composer Create-Project

Alternatively, you may also install Laravel by issuing the Composer create-project command in your terminal:
composer create-project --prefer-dist laravel/laravel blog 

Source: www.laravel.com 

Sunday, 30 April 2017

Laravel 5.4 dropdown default from database

Laravel we have more and more options Like drop-down selection from database records.

Take a simple example permissions and roles. I have to add a role with some permissions.

So I have to call permissions from the database.

RoleController:
$permissions = Permission::where('active_flag', 1)->orderBy('name')->pluck('name', 'id');
return view('roles.create',compact('permissions'));

 roles.create:


  <div class="form-group"> 
 {!! Form::label('permissions', 'Permissions:') !!} 
 {!! Form::select('permissions',$permissions, null,['id'=>'multiple-checkboxes','multiple'=>'multiple'] ) !!}
 </div>


Here I am using multiple dropdown.
Simply we can add roles with permissions.



Tuesday, 25 April 2017

Laravel 5.4 when migrating getting too long error

I can fix this by going in the migration for the user's table, and manually specifying the max-length of the email field, like so:

$table->string('email', 250)->unique();

As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:

use Illuminate\Support\Facades\Schema;
public function boot()
{
    Schema::defaultStringLength(191);
} 

Thursday, 30 March 2017

New feature in laravel 5.4 Route and Model Binding

In this article i will explain route model binding .

Route model binding provides a useful way to automatically method-inject a model's instance into your route closures, or controller actions. For example, if a user visits /users/1, thanks to route model binding, we can make Laravel automatically fetch the Users with an id of 1, and then inject that instance into your controller action.                            

routes/web.php

Route::get('users/{users}', 'UsersController@show');

app/Http/Controllers/UsersController.php

public function show($id)
    {
       $users = User::find($id);
       return $users;

    }



Instead of above code we can bind the model to function. Please find below code.


app/Http/Controllers/UsersController.php

public function show(User $users)
    {
       return $users;

    }

User is a model name and $users form route parameter.

Laravel 5.4 Sub domain routing method with in application

Today I am going to share with you how to define and use sub domain routes better way in Laravel 5 application.

We almost need to create sub-domain of our main domain. But if you are using core PHP or Codeigniter, then you have to create code for new sub-domain and it always take time to develop again and again. However, In Laravel framework 5, they provide group routing that way we can define sub domain routes in same application or project of laravel. We can simply manage sub-domain from our main project that way we don't require to create always new project and code etc. But we can simply manage by single laravel application. so that we can reduce the file size in our server.

In this post i am going give you very simple example of use sub-domain routing of laravel. you have to just follow few step to create simple example from scratch. You can simply create this example in your localhost too.

In this example, we will create one main domain with two sub domain, as listed bellow:

1) laravelpractice.com
2) admin.laravelpractice.com
3) user.laravelpractice.com
Here, as listed above three domain. This example for only local. So i created there three domain. we will simply manage it by single laravel application. So let's proceed.

Step 1 : Install Laravel Fresh Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2 : Add Sub-Domain in ServiceProvider

In this step, we require to register our new two subdomain in RouteServiceProvider, that way we can create new file for each subdomin like we create two sub-domain "admin" and "user" then we create new file admin.php and user.php in routes directory for define routing.

So, let's open RouteServiceProvider.php and put bellow code:

app/Providers/RouteServiceProvider.phpph
namespace App\Providers;
    use Illuminate\Support\Facades\Route;

    use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
    class RouteServiceProvider extends ServiceProvider
    {
    /**
    * This namespace is applied to your controller routes.
    *
    * In addition, it is set as the URL generator's root namespace.
    *
    * @var string
    */
    protected $namespace = 'App\Http\Controllers';
    /**

    * Define your route model bindings, pattern filters, etc.
    *
    * @return void
    */
    public function boot()
    {
    parent::boot();
    }
    /**

    * Define the routes for the application.
    *
    * @return void
    */
    public function map()
    {
    $this->mapApiRoutes();
    $this->mapWebRoutes();
    $this->mapAdminRoutes();
    $this->mapUserRoutes();

    }
    /**

    * Define the "web" routes for the application.
    *
    * These routes all receive session state, CSRF protection, etc.
    *
    * @return void
    */
    protected function mapWebRoutes()
    {
    Route::middleware('web')
    ->namespace($this->namespace)
    ->group(base_path('routes/web.php'));
    }
    /**

    * Define the "api" routes for the application.
    *
    * These routes are typically stateless.
    *
    * @return void
    */
    protected function mapApiRoutes()
    {
    Route::prefix('api')
    ->middleware('api')
    ->namespace($this->namespace)
    ->group(base_path('routes/api.php'));
    }
    /**

    * Define the "admin" routes for the application.
    *
    * These routes are typically stateless.
    *
    * @return void
    */
    protected function mapAdminRoutes()
    {
    Route::namespace($this->namespace)
    ->group(base_path('routes/admin.php'));
    }
    /**

    * Define the "user" routes for the application.
    *
    * These routes are typically stateless.
    *
    * @return void
    */
    protected function mapUserRoutes()
    {
    Route::namespace($this->namespace)
    ->group(base_path('routes/user.php'));
    }

    }

Step 3 : Add Sub-Domain Routes
In Last step, we need to add routes for our main domain and two sub-domain that way we can simply make example. So Here first you have to just add new route in your web.php file for main domain and other two as listed bellow:

1) Admin Routes : Here you have to create new file admin.php in routes folder. in that file you have declare routes for admin sub domain.
2) User Routes : Here you have to create new file user.php in routes folder. in that file you have declare routes for user sub domain.
So, let's proceed with routes define:

routes/web.php

Route::get('/', function () {
dd('Welcome to main domain.');
});

routes/admin.php

Route::get('/', function () {
dd('Welcome to admin subdomain.');
});

routes/user.php

Route::get('/', function () {
dd('Welcome to user subdomain.');
});

Now we are ready to run simple example with our sub-domain. So, in this tutorial i explained i created two subdomain as listed above. So for locally we have to create three domain using virtualhost like as bellow:

1) laravelpractice.com
2) admin.laravelpractice.com
3) user.laravelpractice.com

You have to simple run in your browser.

Tuesday, 28 March 2017

Laravel 5.4 new features and changes

Here are the official changes and new features:

Two new Middleware

We have 2 new middleware: TrimStrings and ConvertEmptyStringsToNull.
  • Trim Strings middleware: automatically trim all the request data.
  • ConvertEmptyStringsToNull: automatically convert all strings to null.

Laravel Elixir will be renamed to Laravel Mix

The next version of Laravel Elixir will be built on Webpack, and it has a new name: Laravel Mix!

Meet the new Laravel Dusk

Laravel Dusk
Dusk is a new browser testing tool of Laravel. Now we can use Dusk to test everything, including Javascript applications (Ajax loading, jQuery, etc.). This is the most awaited feature of Laravel 5.4!

Use any class as Facade

We can use any class as a Facade in Laravel 5.4

Better Vue.js 2 support

Laravel 5.4 supports Vue 2 from the ground. if you love Vue, you're gonna love Vue 2 more!

Use Markdown in our emails

Loving Markdown? With Laravel 5.4, we can use Markdown in our emails. Awesome!

JSON based language files

We can be able to use JSON as our language files. This is great and very useful for multi-language projects.

Add components and slots to our Blade templates

We can add components and slots to our template in Laravel 5.4. Basically, this feature allows us to build reusable HTML elements for our app. If you're working with ReactJS, Angular 2 or Vue; you're right at home.

Minor route improvements

Route system will be enhanced. Route caching system is improved.
Now we can define a named route at the beginning:
Route::name('blog')->get('blog/{id}/', function ($id) {
});

Upgrade to Laravel 5.4

You can easily upgrade to Laravel 5.4 by following this guide:
https://laravel.com/docs/5.4/upgrade
Basically, you can just update your laravel/framework dependency to **5.4.* in your composer.json** file.
Find:
"laravel/framework": "5.3.*",
Change to:
"laravel/framework": "5.4.*",
And then run this command:
composer update
Please note that Elixir and some features have been changed. So if you still need some old features, don't upgrade yet.

Laravel 5.4's release notes

Learn more about all new features at:
https://laravel.com/docs/5.4/releases

Source Code

Laravel 5.4's source code is now available at:
https://github.com/laravel/framework

Final thoughts

Awesome! Let's start building your apps with Laravel 5.4!

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles