Showing posts with label Laravel 5.x. Show all posts
Showing posts with label Laravel 5.x. Show all posts

Friday, February 8, 2019

Try catch not working Laravel raw queries | How to properly catch PHP exceptions (Laravel 5.X) | Why try catch not work in Laravel


Make sure you're using your namespaces properly. If you use a class without providing its namespace, PHP looks for the class in the current namespace. Exception class exists in global namespace, so if you do that try/catch in some namespaced code, e.g. your controller or model, you'll need to do catch(\Exception $e) to catch all your exceptions:  


try {
  //code causing exception to be thrown
}
catch(\Exception $e) {
  //exception handling
}


If you do it like this there is no way to miss any exceptions. Otherwise if you get an exception in a controller code that is stored in App\Http\Controllers, your catch will wait for App\Http\Controllers\Exception object to be thrown.

Getting url() in a Command returns http://localhost | URL generated in a queue - localhost returned | URL in jobs only return localhost | URL problem with queued jobs

First need to specify url to project/config/app.php as below:

<?php
return [
    'url' => 'http://localhost:81/my_laravel_project/'
];

Now need to modify project/app/Providers/RouteServiceProvider.php as below:


<?php
namespace App\Providers;

use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to the controller routes in your routes file.
     *
     * 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.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function boot(Router $router)
    {
        parent::boot($router);

        $url = $this->app['url'];
        $url->forceRootUrl(config('app.url'));
    }

    /**
     * Define the routes for the application.
     *
     * @param  \Illuminate\Routing\Router  $router
     * @return void
     */
    public function map(Router $router)
    {
        $router->group(['namespace' => $this->namespace], function ($router) {
            require app_path('Http/routes.php');
        });
    }
}

Laravel :: Getting url() in a Command returns http://localhost | URL problem with queued jobs | Url helper functions that are relative to application url config | Localhost url problems

First need to configure project/config/app.php as below:
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/

'url' => 'http://localhost:81/my_project/'
Now need to edit project/app/Providers/AppServiceProvider.php as below:
namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;


class AppServiceProvider extends ServiceProvider
{
    private static $logStream;


    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        URL::forceRootUrl(Config::get('app.url'));
    }
}

Middleware when running Artisan console request

I want a custom piece of middleware to run whenever I run an artisan command. I need to set a few environment variables to be used in the app configuration before the actual command executes.
There is a way around but not sure if the correct one, but works. Just open up the Kernel class and override the bootstrap method. Laravel 5.3, have not tested on other versions, but should work similarly
<?php
namespace App\Console;

use Log;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        Commands\Inspire::class,
    ];

    public function bootstrap() {
        parent::bootstrap();
        Log::info("KERNEL COMMAND EXECUTING");
    }

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->call('App\Service\YourService@method')->dailyAt('10:00');

        $schedule->call('App\Service\YourService@method')->everyFiveMinutes();
    }
}
And finally the Kernel is executed by CronJob or Laravel Jobs.
To add Laravel kernel scheduler to CronJob, execute command crontab -e and add below line and save the operation.
* * * * * /usr/local/bin/php /project_location/www/project/artisan schedule:run >> /dev/null 2>&1

Sunday, June 25, 2017

Laravel 5: How to Select from Sub-query using Laravel Query Builder

Laravel 5: How to Select from Sub-query using Laravel Query Builder. It's very easy to use Sub-Query as Select query in Laravel. Below is a sample code snippet:



$query = DB::table("employee")->whereIn("id", array(1, 2, 3, 4));
$result = DB::table(DB::raw("(" . $query->toSql() . ") as res"))
    ->mergeBindings($query)
    ->whereIn("res.id", array(2, 3))->get();

Which will generate below SQL:

select * from (select * from `employee` where `id` in (1, 2, 3, 4)) as res 
where `res`.`id` in (2, 3))

Friday, June 23, 2017

Laravel 5.X: How Do I Get Raw SQL Query As String With Binders From Query Builder | Query Builder To Raw SQL

Laravel 5.X: How Do I Get Raw SQL Query As String With Binders From Query Builder. Its very easy and now few steps from here. It's interesting.


use Illuminate\Support\Facades\DB;

$model = DB::table('employee')
    ->where("employee.id", "<>", 0)
    ->whereIn("employee.id", array(1, 2))
    ->where("employee.type", "PERMANENT")
    ->where(function ($q) {
        $q->where("employee.id", 1)->orWhere("employee.id", 2);
    })
    ->limit(10)->offset(0)
    ->groupBy("employee.id")
    ->select("employee.id");

$replace = function ($sql, $bindings) {
    $pos = 0;
    $needle = '?';
    foreach ($bindings as $replace) {
        if (is_string($replace)) {
            $replace = DB::connection()->getPdo()->quote($replace);
        }
        $pos = strpos($sql, $needle, $pos);
        if ($pos !== false) {
            $sql = substr_replace($sql, $replace, $pos, strlen($needle));
            $pos = $pos + strlen($replace);
        }
    }
    return $sql;
};

$sql = $replace($model->toSql(), $model->getBindings());
echo "SQL=" . $sql;

And output would be like below:

SELECT `employee`.`id`
FROM `employee`
WHERE `employee`.`id` <> 0 AND `employee`.`id` IN (1, 2) AND
      `employee`.`type` = 'PERMANENT' AND (`employee`.`id` = 1 OR `employee`.`id` = 2)
GROUP BY `employee`.`id`
LIMIT 10 OFFSET 0

Laravel 5.X: Use of SubQuery | Sub Query in Where Condition

Laravel 5.X: Use of Sub-query | Sub Query in Where Condition. It's very important. Below is an sample example how we can use sub-query in our application.



$test_data = Employee::from((new Employee())->getTable() . " AS entity")

OR

$test_data = DB::table((new Employee())->getTable() . " AS entity")
    ->whereIn("entity.id", function($q) {
        $q->select("entity.id");
        $q->from((new Employee())->getTable() . " AS entity");
        $q->join((new Supervisor())->getTable() . " AS user", "user.id", "=", "entity.sup_id");
        $q->whereNotNull("entity.sup_id");
        $q->where("user.first_name", "like", "%E%");
    })
    ->select("entity.id", "entity.type")
    ->limit(5)
    ->get();


And will generate below SQL:


SELECT
  `entity`.`id`,
  `entity`.`type`
FROM `employee` AS `entity`
WHERE `entity`.`id` IN (
  SELECT `entity`.`id`
  FROM `employee` AS `entity` INNER JOIN `users` AS `user` ON `user`.`id` = `entity`.`sup_id`
  WHERE `entity`.`sup_id` IS NOT NULL AND `user`.`first_name` LIKE '%E%'
)
LIMIT 5

Thursday, June 22, 2017

Laravel 5:Redirect back to the same page where the request comes from with input and messages

In your controller:

use Illuminate\Support\Facades\Redirect;

$error[] = "SOME ERRORS";
return Redirect::back()
    ->withErrors(compact('error'))
    ->with("reason1", "SOME REASON 1")
    ->with("reason2", "SOME REASON 2")
    ->withInput();


And in your view file:

<div>Reason1: {{ session('reason1') }}</div>

<div>Reason1: {{ session('reason2') }}</div>

Option "withInput" will fill back up input fields with previous data:

{!! Form::label('name', 'Name:') !!}

Sunday, June 18, 2017

Laravel 5.X Forward To URL | Redirect To Route | Forward Request | Mock Request | Mock HTTP Request | Dynamic HTTP Request | Laravel Dispatch Request

Laravel 5.X Forward To URL | Redirect To Route | Forward Request | Mock Request | Mock HTTP Request | Dynamic HTTP Request.

It's easy to forward a request using Laravel Route. Forwarding do for you is URL will not change but response will change. The way it is working is that it will mock a request using Request::create(...) and dispatch using current Router.

<?php
namespace App\Http\Controllers;

use Illuminate\Routing\Router;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request as HttpRequest;

class MyController extends Controller {
    public function __construct(HttpRequest $httpRequest, Router $router) {
        $this->middleware('auth', ['except' => ['action1', 'action2']]);

        $this->httpRequest = $httpRequest;
        $this->appRouter = $router;
    }

    //Route=my/need-forward (Create new request and dispatch)
    public function needForward() {
        //http://www.domain.com/my/need-forward
        $route_name = action("MyController@forwardHere");
        //http://www.domain.com/other-folder/my/need-forward
        $route_name = action("OtherFolder\\MyController@forwardHere");
        //http://www.domain.com/my/need-forward/100/Name%20Text?roll=303
        $route_name = action("MyController@forwardHere", ["id" => 100, "name" => "Name Text", "roll" => 303]);
        $request = HttpRequest::create($route_name, 'GET', array("param1" => "Param 1", "param2" => "Param 2"));
        return app()->handle($request);
    }

    //Route=my/need-forward (Forward same request to new route)
    public function needForward2() {
        $route_name = "my/forward-here";
        $request = HttpRequest::create($route_name);
        return  $this->appRouter->dispatch($request);
    }

    //Route=my/forward-here
    public function forwardHere() {
        echo "Browse [/my/need-forward] will send output of [/my/forward-here]";
    }
}


Friday, June 16, 2017

How to Handle Laravel 5.X Exceptions

In Laravel 5.2, all errors and exceptions, both custom and default, are handled by the Handler class in app/Exceptions/Handler.php with the help of two methods ##report & ##render.

The ##report method enables you to log raised exceptions or parse them to error logging engines. 

The ##render method return with an error message as well as response code raised by any exception. It generates a HTTP response from the exception and sends it back to the browser.

We can also override the default behavior of error handling with our own custom exception handler if we need.


/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request $request
 * @param  \Exception $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    //$request->ajax() [[AJAX REQUEST CHECK]]
    if ($e instanceof MyException) {
        if ($request->ajax()) {
            return response()->json(['error' => 'Not Found'], 404);
        }
        return response()->view('errors.custom', [], 500);
    }
    if ($this->isHttpException($e)) {
        switch ($e->getStatusCode()) {
            case 404:
                return response()->view('errors.404', [], 500);
            case 500:
                return response()->view('errors.500', [], 500);
            default:
                return $this->renderHttpException($e);
        }
    } elseif ($e instanceof TokenMismatchException) {
        return redirect()->back()->withInput()->with('error', 'Your session has expired');
    } else {
        return response()->view('errors.500', [], 500);
    }
}

And finally you have to add a 404.blade.php && 500.blade.php file in resources/view/errors to represent our custom error page.


We can also generate a 404 as well as 500 error page response by calling the abort method which takes an optional response message.

abort(404, 'The resource you are looking for could not be found');


This will check for a related resources/view/errors/404.blade.php and serve a HTTP response with the 404 status code back to the browser. The same applies for 401 and 500 or for other error status codes.








Laravel 5.X: Access HTTP Session From Controller | Service

It's very important to have access to HTTP session in our Laravel Controller or Service class. Below is a code sample of session access in Controller class. Use of session in Service is similar.


<?php

namespace App\Http\Controllers;

use Symfony\Component\HttpFoundation\Session\Session;

class HomeController extends BaseController
{
    private $Session;
    public function __construct(Session $Session)
    {
        $this->Session = $Session;
    }

    public function setSessionValue()
    {
        $this->Session->set("some_name", "some_value_in_session");
        exit;
    }

    public function getSessionValue() {
        echo $this->Session->get("some_name");
        die();
    }










Laravel 5.X: Access HTTP Request From Controller | Service

It's very important to access HTTP Request from Laravel Controller or Service. Below is a code example showing how to access HTTP Request from Laravel Controller. Same for Service.


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request as HttpRequest;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    private $HttpRequest;
    public function __construct(HttpRequest $HttpRequest)
    {
        $this->HttpRequest = $HttpRequest;
    }

    public function checkHttpRequest() {
        echo $this->HttpRequest->get("id") . "<BR>";
        echo $this->HttpRequest->getBaseUrl() . "<BR>";
        echo $this->HttpRequest->getUri();
        die();
    }
}









Monday, June 12, 2017

Laravel 5.X Authentication Custom User Providers

The custom provider let you make and bridge between your user custom authentication service and the Laravel functionality. It's easy to implement, below show full process step by step.

Stage 1: Create a User model class ->

At first you need to create a model class which will extend laravel custom model Illuminate\Foundation\Auth\User as below:

<?php
namespace App\Entity;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are assignable.
     */
    protected $fillable = [
        'first_name', 'last_name'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     */
    protected $hidden = [
        'password', 'remember_token'
    ];
}


Stage 2: Create a CustomValidationProvider (Data Checker) class ->


<?php
namespace App\Authentication;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider;

class CustomValidationProvider implements UserProvider
{
    public function __construct($model)
    {
        $this->model = $model;
    }

    public function retrieveById($identifier)
    {
        /* Get by id */
        return $this->createModel()->newQuery()->find($identifier);
    }

    public function retrieveByToken($identifier, $token)
    {
        $model = $this->createModel();

        return $model->newQuery()
            ->where($model->getAuthIdentifierName(), $identifier)
            ->where($model->getRememberTokenName(), $token)
            ->first();
    }

    public function updateRememberToken(Authenticatable $user, $token)
    {
        $user->setRememberToken($token);
        $user->save();
    }

    public function retrieveByCredentials(array $credentials)
    {
        $query = $this->createModel()->newQuery();

        foreach ($credentials as $key => $value) {
            if (!contains($key, 'password')) {
                $query->where($key, $value);
            }
        }

        return $query->first();
    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        /* Match password here */
        return $user->getAuthPassword() == $credentials["password"];
    }

    public function createModel()
    {
        $class = '\\'.ltrim($this->model, '\\');
        return new $class;
    }

    static function contains($haystack, $needle)
    {
        return strpos($haystack, $needle) !== false;
    }
}

Don’t forget to register this service in your config/app.php file as below line:


'providers' => [
    App\Authentication\CustomAuthServiceProvider::class,
    ......
]

Stage 3: Register custom provider (Initialization of Provider) ->

<?php
namespace App\Authentication;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;

class CustomAuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Auth::provider('custom_provider', function($app, array $config) {
            return new CustomValidationProvider($config['model']);
        });
    }

    public function register()
    {

    }
}

Stage 4: Update config to use new driver ->

Open up your config/auth.php file and scroll down to the "providers" section, and simply update the driver name as shown:


'providers' => [
    'users' => [
        'driver' => 'custom_provider',
        'model' => App\Entity\User::class,
    ]
]


Again, I can’t tell you how to implement these methods as it depends on how and where you’re storing your users, but as an example, you might inject an API provider via the constructor, and then make API calls to get user data, apply it to an instance of our user class, then return it.



Monday, May 15, 2017

How to add header/footer to multipage pdf on Laravel SnappyPDF WkHtmlToPdf

I am sure you already install WkhtmlToPDF with Snappy in your Laravel project. If not yet please visit the link below to install WkhtmltoPDF in your Laravel project:

Click here.

Now add margin for your page if not added yet because header and footer need some space to visible in PDF header and footer area by editing the file named "config/snappy.php".

<?php
return array(
    'pdf' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'timeout' => false,
        'options' => array(
            'margin-top'    => 15,
            'margin-right'  => 8,
            'margin-bottom' => 20,
            'margin-left'   => 5
        ),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltoimage.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    )

);


And then append header and footer html in your PDF as follows:

$header_html = file_get_contents(resource_path() . "/" . "pdf-header.html");
$footer_html = file_get_contents(resource_path() . "/" . "pdf-footer.html");

$pdf = SnappyPDF::loadView('home.pdf');
$pdf->setOption('header-html', $header_html);
$pdf->setOption('footer-html', $footer_html);

return $pdf->stream('invoice.pdf');

You have to create two file named "pdf-header.html" and "pdf-footer.html" in your "resource" folder as below.

pdf-header.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body style="border: 1px solid blue;margin-left: 18px;margin-right: 0px;padding:10px">
<div style="color: red">
    THIS IS <b>PAGE HEADER</b>
</div>
</body>

</html>

pdf-footer.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body style="border: 1px solid blue;margin-left: 18px;margin-right: 0px;padding:10px">
<div style="color: red">
    THIS IS <b>PAGE FOOTER</b>
    Page=<span id="page"></span>|
    Total Page=<span id="topage"></span>|
    Date=<span id="date"></span>|
    Time=<span id="time"></span>
</div>
<script type="text/javascript">
    var vars={};
    var x=window.location.search.substring(1).split('&');
    for (var i in x) {
        var z=x[i].split('=',2);
        vars[z[0]] = unescape(z[1]);
    }
    document.getElementById('page').innerHTML = vars.page;
    document.getElementById('topage').innerHTML = vars.topage;
    document.getElementById('date').innerHTML = vars.date;
    document.getElementById('time').innerHTML = vars.time;
</script>
</body>

</html>




PDF will look like below images:




Friday, April 21, 2017

Generate PDFs in Laravel 5.x with Snappy & Wkhtmltopdf

At first we need to install Wkhtmltopdf on our server. To do so we need to add following lines under "require" section on composer.json file as follows:

For 32 bit server:



"require": {
    "h4cc/wkhtmltopdf-i386": "0.12.x",
    "h4cc/wkhtmltoimage-i386": "0.12.x"
}




And for 64 bit server:


"require": {
    "h4cc/wkhtmltopdf-amd64": "0.12.x",
    "h4cc/wkhtmltoimage-amd64": "0.12.x"
}


Now time to install wkhtmltopdf. Open command prompt and navigate to your project location and execute below command to install packages into your project:

composer update h4cc/wkhtmltopdf-i386 --lock
composer update h4cc/wkhtmltoimage-i386 --lock
Or you can download wkhtmltopdf from here. Or you can download also from here.

For windows you do not do anything above. Just download installer for windows version, install it in your machine, done. 

After installation done, need to install "Laravel-Snappy", to do so add following to "composer.json" as follows:


"require": {
    "barryvdh/laravel-snappy": "0.3.x"
}

Open command prompt and navigate to your project location and execute below command to install packages into your project:

composer update barryvdh/laravel-snappy --lock

Next step is to add the line "Barryvdh\Snappy\ServiceProvider::class" to file "config/app.php" under the section "providers" as follows:


'providers' => [Barryvdh\Snappy\ServiceProvider::class]

Its time to add an alias, do the following in "config/app.php" under "aliases" section:


'aliases' => [
    'SnappyPDF' => Barryvdh\Snappy\Facades\SnappyPdf::class
]

Now copy "vendor/barryvdh/laravel-snappy/config/snappy.php" file to "/config" folder and modify as follows:

Location of binary is your wkhtmltopdf installation location. You must careful with the path.

Binary path will be as follows:

For Linux Server with project location: "binary" => ...
base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64')

For Linux Server with fixed location: "binary" => ...
"/usr/local/bin/wkhtmltopdf-amd64"

And for Windows with installation location: "binary" => ...
'"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf\bin\wkhtmltopdf.exe"'

<?php
return array(
    'pdf' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltopdf.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    ),
    'image' => array(
        'enabled' => true,
        'binary'  => 'C:\PROGRA~1\wkhtmltopdf\bin\wkhtmltoimage.exe',
        'timeout' => false,
        'options' => array(),
        'env'     => array(),
    )

);

Execute following command:
php artisan vendor:publish

Below is a code snippet to stream pdf to browser:

<?php
namespace App\Http\Controllers;

use SnappyPDF;
use Illuminate\Routing\Controller as BaseController;

class HomeController extends BaseController
{
    public function pdf()
    {
        set_time_limit(60 * 5);
        $d1 = array();
        $d2 = array();

        $pdf = SnappyPDF::loadView('home.pdf', compact('d1', 'd2'));

        //Below line code will save pdf to "public" folder
        //$pdf->save('Output.pdf'); die();

        //Below line code to download pdf
        //return $pdf->download('Output.pdf');

        //Below line code will stream PDF to browser
        return $pdf->stream('Output.pdf');
    }

}

And thats it, you have integrated Wkhtmltopdf in your Laravel project using Snappy.