Friday, January 22, 2016

Add extra column in migration after creating a table in Laravel 5.2

Hello Everyone,

Today, we will discuss about how to add an extra column in migration after creating a table.

First time we create a table's migration & execute migration command. After this , we realize that we have to add a new column in table.

For this, we can modify table by this easy way.

Firstly run this command for create a new migration file.

php artisan make:migration add_column_name_in_users_table

It will create a new file in migration file. Write in its up() function the following lines.

 Schema::table('users', function ($table) {
            $table->string('column_name');
          });


and in down() function write the following lines:

Schema::table('users', function ($table) {
            $table->drop_column('
column_name');

          });

Make sure you put the same table name which you want to edit.

Now run migration command for this file.

php artisan migrate

Thanks.

Friday, January 15, 2016

Defining middleware in laravel

HTTP middleware provide a convenient mechanism for filtering HTTP requests entering our application. For example, Laravel includes a middleware that verifies the user of our application is authenticated. If the user is not authenticated, the middleware will redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

There are several middleware included in the Laravel framework, including middleware for maintenance, authentication, CSRF protection, and more. All of these middleware are located in the app/Http/Middleware directory.


Defining Middleware

To create a new middleware, use the make:middleware Artisan command:

php artisan make:middleware OldMiddleware

This command will place a new OldMiddleware class within our app/Http/Middleware directory. In this middleware, we will only allow access to the route if the supplied age is greater than 20. Otherwise, we will redirect the users back to the "home" URI.

<?php

namespace App\Http\Middleware;

use Closure;

class OldMiddleware
{
    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->input('age') <= 20) {
            return redirect('home');
        }

        return $next($request);
    }

}
As we can see, if the given age is less than or equal to 20, the middleware will return an HTTP redirect to the client; otherwise, the request will be passed further into the application. To pass the request deeper into the application (allowing the middleware to "pass"), simply call the $next callback with the $request.

It's best to envision middleware as a series of "layers" HTTP requests must pass through before they hit our application. Each layer can examine the request and even reject it entirely.

Before / After Middleware

Whether a middleware runs before or after a request depends on the middleware itself. For example, the following middleware would perform some task before the request is handled by the application:

<?php

namespace App\Http\Middleware;

use Closure;

class BeforeMiddleware
{
    public function handle($request, Closure $next)
    {
        // Perform action

        return $next($request);
    }
}
However, this middleware would perform its task after the request is handled by the application:

<?php

namespace App\Http\Middleware;

use Closure;

class AfterMiddleware
{
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        // Perform action

        return $response;
    }
}

Friday, January 8, 2016

How to implement quickbooks online in laravel 5.1

Hi, Today we will learn how to integrate quickbooks online in laravel 5.1.
The very first thing we'll have to do is register our app with Intuit. When we do this, Intuit will give us these variables:
  • app token
  • consumer secret
  • consumer key

 The installation

Add this to your composer.json.
"require": {    
    "consolibyte/quickbooks": "^3.1"   
},

This will give us a page in root folder, QuickBooks.php. Include this page in public/index.php.

require_once '../QuickBooks.php';

Now make a controller in app/Http/Controllers

QuickBookController.php.

Write following code to your controller:

class QuickBookController extends Controller
{
    public function __Construct()
    {
        $oauth_consumer_key = 'your consumer key';
        $oauth_consumer_secret = 'consumer secret';
        $sandbox = true;     // When you're using development tokens
        $dsn = 'mysqli://root:root@localhost/DbName';
        $encryption_key = 'encryption key';
        $the_username = 'DO_NOT_CHANGE_ME';
        $the_tenant = 12345;

        if (!\QuickBooks_Utilities::initialized($dsn)) {
            // Initialize creates the neccessary database schema for queueing up requests and logging
            \QuickBooks_Utilities::initialize($dsn);
        }
        $IntuitAnywhere = new \QuickBooks_IPP_IntuitAnywhere($dsn, $encryption_key, $oauth_consumer_key, $oauth_consumer_secret, '', '');

        if ($IntuitAnywhere->check($the_username, $the_tenant) && $IntuitAnywhere->test($the_username, $the_tenant)) {
            // Set up the IPP instance
            $IPP = new \QuickBooks_IPP($dsn);
            // Get our OAuth credentials from the database
            $creds = $IntuitAnywhere->load($the_username, $the_tenant);
            // Tell the framework to load some data from the OAuth store
            $IPP->authMode(
                \QuickBooks_IPP::AUTHMODE_OAUTH,
                $the_username,
                $creds);

            if ($sandbox) {
                // Turn on sandbox mode/URLs
                $IPP->sandbox(true);
            }
            // This is our current realm
            $this->realmId = $creds['qb_realm'];
            // Load the OAuth information from the database
            $this->context = $IPP->context();
        } else {
            return false;
        }
    }

    public function getCustomerCount()
    {
        //echo csrf_token();
        $VendorService = new \QuickBooks_IPP_Service_Vendor();

        $vendors = $VendorService->query($this->context, $this->realmId, "SELECT * FROM Customer");
        print_r($vendors);
        exit;
        foreach ($vendors as $Vendor) {
            print('Vendor Id=' . $Vendor->getId() . ' is named: ' . $Vendor->getDisplayName() . '<br>');
        }
    }


}

The constructer will give connection to quickbook online account. After that you can make function to your use like getCustomerCount().