Powered by Blogger.

Search This Blog

Sunday, 9 October 2016

Unknown

Create CRUD module in just single command,with no need to import package at Easylaravel

Create CRUD module in just single command,with no need to import packages etc.

At EasyLaravel ,we have came up with amazing idea to make laravel developers life little bit easier.We have created single command that will create CRUD module with forms controller and much more.

With one command you will get :

  1. Migration created by the name you specify
  2. Model with same name of module in singular form as we make at laravel
  3. Controller with functions created -
    1. Index
    2. Create
    3. Store
    4. Edit
    5. Update
    6. Delete

Steps to make this thing possible -

Step 1 - Create new project

Create new laravel 5.3 project - using
composer create-project laravel/laravel Crud

Step 2- Download file from this link - Click here

Once downloaded copy and paste the file in App\Consoles\Commands directory


Step 3- Open App\Console\Commands\Kernel.php file

Opern Kernel.php file and Register your command ,here is code -


    protected $commands = [
        //

      Commands\CRUDCommand::class
    ]; 
    
   

Step 4- Run php artisan command and check whether make:crud command is generated .


Step 5 -Before running command,lets take a look at what parameters the command accepts

php artisan make:crud Module_name --table=table_name --model=Model_name --fields=Form field that are created in views

Eg-

php artisan make:crud posts --table=posts --model=Post --fields=name,email,mobilenumber

Above command accepts parameters as follows

  1. posts= module name you want to give
  2. --table=Table name - which creates migration with table name defined
  3. --model= Name of model you want to create
  4. --fields= list of field separated by ' , ', this fields are automatically created in views

Note :

  • Routes are create in routes\web.php
  • Controller as - Module-Name\BaseController.php in controller directory


Now lets see what happens when we run this command
php artisan make:crud posts --table=posts --model=Post --fields=name,email,mobilenumber

Future Enhancement

  1. Add code in controller for all functions
  2. Creating Model with table name in it
  3. Perfect documentation

What's missing

We have created this CrudCommand for laravel 5.3 application only. If you want to implement in laravel 5.2 or previous version then change the path of routes file where file contents are appended .

Thank You, If you have any doubts please comment .Like and share to make everyone life easier.

Read More

Tuesday, 13 September 2016

Unknown

Debug and log query using DB::enablequerylog() in laravel

Debugging query in laravel

    Steps for Logging query in Log file
  1. First use DB;
  2. DB::enableQueryLog();
  3. you can dd(DB::getQueryLog())
  4. To get last executed query use - end(DB::getQueryLog())
     DB::enableQueryLog();
       // Run your queries
       // ...

       // Then to retrieve everything since you enabled the logging:
       $queries = DB::getQueryLog();
       foreach($queries as $i=>$query)
       {
           Log::debug("Query $i: " . json_encode($query));
       }

       Log::debug('Last query is :'.$last_query = end($queries););
  


In next post we will learn more about relationship methods

  1. with()
  2. has()
  3. whereHas()
  4. withCount()

Thank you.

Read More

Thursday, 1 September 2016

Unknown

List of eloquent Relationships in laravel -Part 1

How relationships work in laravel

Lets understand this concept using following example,there is blog where user creates many posts -

There are 2 tables - 1.users, following are the fields-

Users table model name- User
  1. id
  2. name
  3. email
2.posts,following are the fields-
Posts table model name- Post
  1. id
  2. body
  3. desc
  4. user_id

Below is User Model,since user can create many posts so we implement hasMany() relationship in model

Eloquent will automatically determine the proper foreign key column on Post Model

    namespace App;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    class User extends Authenticatable
    {
        public function post()
        {
            return $this->hasMany('App\POst');
        }
    }
    

Below is Post Model,since each post belongs to speccific user, we implement belongsTo() relationship.

First argument of belongsTo() is path of model , second argument is foreign_key here its user_id

    namespace App;
    
    class Post
    {
        public function user()
        {
            return $this->belongsTo('App\User','user_id');
        }
    }
    

Lets tinker it >

  1. Run composer and type php artisan tinker
  2. $user=App\User::find(1) - it gives user record whose id is 1
  3. To get all post related to $user we write > $posts=$user->posts()->get()
Lets tinker it to get user related to post >
  1. Run composer and type php artisan tinker
  2. $post=App\Post::find(1) - it gives Post record whose id is 1
  3. To get user to whom this post belongs we write > $user=$post->user()->first()
  4. we can retrieve use by accessing Eloquent relationships as properties > $user=$post->user

In next post we will learn more about relatonships -

  1. hasMany()
  2. hasOne
  3. Many to Many

Thank you.

Read More

Monday, 29 August 2016

Unknown

Whats cool about dependency injection in laravel 5.3 at easylaravel

What is dependency injection ??

If you want to use any functionality or components in your controller like send mail ,you register that class in controller and then create its object in every function but when we want that functionality in every function or methods of controller then you use dependency injection


Create dependency injection ,follow steps -

  1. Register component

    Register that component class using - use Class eg- use Mailer;

  2. Create variable in class and initialize it in constructor as follow :
    				protected $mailer;
    				public function __construct(Mailer $mailer)
    				{
    				$this->mailer = $mailer;
    				}
    			
    Here, now depenedncy is injected in constructor and we access it using $this->mailer; Dependency can be anyclass even model,like -
    				protected $user;
    				public function __construct(User $user)
    				{
    				$this->user = $user;
    				}
    			
    ,now we dont need to create model object in function like
    	
    			public function store()
    			{ 		
    			$user= new User;  
    			}
    		
    we do it as -
    			public function store()
    			{ 
    			$user= $this->user;
    			$user->name='ABC';
    			$user->save();
    			}
    	

Thank you.

Read More

Sunday, 28 August 2016

Unknown

Part 1- Create command line apps using make:command laravel 5.3 at easylaravel

What is artisan ??

Artisan is commandline interface to create controller ,migration,model and also our own commands that will do specific task.Artisan provides list of commands

type : php artisan list

Create command ,follow steps -

  1. Create Command -
    Run composer and type - " php artisan make:command Say " ,
    command will be created in console/commands/Say.php
  2. Edit your command file and add logic that needs to be executed -
    In Say.php edit -
    1. Edit name and signature of the console command.edit -
      protected $signature = 'Say:hello {user}';
      {user} - command arguments
      eg- 'php artisan Say:hello Omkar'; ,here omkar is argument
    2. You can add dependencies inside constructor and laravel will take care of rest of it.
      protected $user;
      public function __construct(User $user) {
      parent::__construct();
      $this->user = $user;
      }
    3. Write command logic i.e the code that you want to execute when command runs in artisan
      public function handle()
      {
      $this->comment('Hello : '.$this->user);
      }
  3. Register command
    Register command in Console\kernel.php protected $commands = [ Commands\Say::class ];
  4. Run Command -
    php artisan Say:hello

Thank you, Part 2 will be posted soon.

Read More

Monday, 22 August 2016

Unknown

Encrypt Data before someone hacks using Crypt facade - Easylaravel

Introduction :

Encryption of information on web is very crucial part nowdays , laravel comes with Crypt facade to implement encryption in laravel.

Usage:

  • Step 1 : First set key in config\app.php with random string . key should be 32 characters
  • Step 2 : Use Crypt facade for encryption and decryption.

Encryption :

$encrypted_value=Crypt::decrypt($request->password);

Decryption :

$decrypted_value=Crypt::decrypt($encrypted_value);

Example :


namespace App\Http\Controllers;
use Crypt;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\DecryptException;
class UserController extends Controller

{
public function storeSecret(Request $request, $id)
{
$user = User::findOrFail($id);
$encryptedValue=Crypt::encrypt($request->secret);
$user->fill([
'secret' =>$encryptedValue
])->save();
echo ‘Encrypted value : ’.$encryptedValue.’
’;
try {
$decrypted = Crypt::decrypt();
echo ‘decrypted value : ’.$decrypted.’
’; } catch (DecryptException $e) {
}
} }

Read More

Thursday, 11 August 2016

Unknown

How request is handled in laravel -easylaravelBlog

How request is handled in laravel

When request to laravel application is made ,that requests are directed to public/index.php directed by web server.Then index.php loads composer generated autoloader definition,and then instance of laravel application is retrieved from bootstrap/app.php

HTTP / Console Kernels

Once we get instance of application,incoming request is sent to HTTP kernel or console kernel ,depending upon type of request. HTTP kernel is located inapp/Http/Kernel.php. The HTTP kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array ofbootstrappers that will be run before the request is executed. These bootstrappers configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request is actually handled.
The HTTP kernel also defines a list of HTTP middleware that all requests must pass through before being handled by the application. These middleware handle reading and writing the HTTP session, determine if the application is in maintenance mode, verifying the CSRF token, and more.

Service Providers

One of the most important Kernel bootstrapping actions is loading the service providers for your application. All of the service providers for the application are configured in the config/app.phpconfiguration file's providers array. First, the register method will be called on all providers, then, once all providers have been registered, the boot method will be called. Dispatch Request

Response

Once the application has been bootstrapped and all service providers have been registered, theRequest will be handed off to the router for dispatching. The router will dispatch the request to a route or controller, as well as run any route specific middleware.
Read More