Friday, April 29, 2016

AND-OR-AND + brackets with Eloquent-Laravel 5.1

AND-OR-AND + brackets with Eloquent-Laravel 5.1:

Eloquent is a great thing – you can build your query step-by-step and then call get() method. But sometimes it gets a little tricky for more complicated queries – for example, if you have multiple AND-OR conditions and you want to put brackets, how to do it properly?

Wrong way – easy to make mistake

Let’s say we need to filter male customers aged 18+ or female customers aged 65+ (whatever the reason is, it’s just a hypothetical example). Simple MySQL query would look something like this:

.... WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and age >= 65)
Now let’s transform it to Eloquent:

//............
$q->where('gender', 'Male');
$q->orWhere('age', '>=', 18);
$q->where('gender', 'Female');
$q->orWhere('age', '>=', 65);


But wait, if we launch it like that, MySQL query wouldn’t have any brackets and would be launches as this:


....WHERE gender = 'Male' and age >= 18 or gender = 'Female' and age >= 65
Which is wrong order – it would actually be executed in this order:


.....WHERE ((gender = 'Male' and age >= 18) or gender = 'Female') and age >= 65

The worst thing is that it wouldn’t throw any errors. And if you don’t test properly, you wouldn’t even notice that it filtered out wrong results.

Right way – putting “brackets” into Eloquent

What we actually need here is a thing called Advanced Where Clauses – where we can assign a function to a where clause, like this:

//......
$q->where(function ($query) use ($gender, $age) {
    $query->where('gender', $gender)
        ->where('age', '>=',  $age;
})->orWhere(function($query) use ($gender, $age) {
    $query->where('gender',  $gender)
        ->where('age', '>=',  $age);
})

This code will produce SQL query exactly like we need – with brackets in the right places.

Thanks.

Friday, April 15, 2016

How to generate random password in Laravel

How to generate random password in Laravel:

 This is a really really short one. I just needed to generate a random password for a user and noticed that Google doesn’t really give a one-line answer, which is really simple.

Firstly, how to generate random string – Laravel has a helper called str_random($length). In terms of saving hashed password to database, we use Hash::make($password).

So the end result for generating 8-symbol length password looks as simple as this:

$hashed_random_password = Hash::make(str_random(8));

Of course, don’t forget:
use Illuminate\Support\Facades\Hash;

Generated password can look like:

JnR390XB
2I2so1Wr
oVNBAic9

And hash is something like:

$2y$10$E1.y.jjmr7ecmMAiwFFQMO6KXn0scLB2hvVBbx8cny.lREZ6xdDMW
$2y$10$pVOI0me9sLPRbyQnxNQrDurSoJScsE6nvu6VVOsMDF8qusH7HUlo2
$2y$10$xVPUPgm1vpUTpZ8jAJE/Xeh7j8G8EFnlQYp27Zjy8qnabqessSc2i

That’s it for now, hope it’s a useful short tip.
Thanks.

Friday, April 8, 2016

Log every request & response in Laravel 5.1

Log every request & response in Laravel 5.1:

Sometimes it's useful to log some/all requests to your application. This is really convenient when you use Laravel to build your APIs.

A logging middleware might log all incoming requests to your application. In Laravel 5.1 there is a terminate method and it's call after the sending HTTP response to the browser. This way we have access to both $request and $response at the same time.

Let's take a look at how we are going to achieve this in Laravel 5.1:

Create a new middleware by type this command in terminal inside your project directory.

php artisan make:middleware LogAfterRequest

And then put below code in it(project_dir/app/Http/Middleware/LogAfterRequest.php).

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogAfterRequest {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
    }

}


In this code terminate method receives $request and $response. These are objects which will give you all of the handy stuffs you probably need to log.

When we have our middleware ready, we should add it to our HTTP Kernel.

Open Kernel.php and add this line to your protected $middleware property:

\App\Http\Middleware\LogAfterRequest::class

That's it. You can additionally filter what you want to actually log, but this is the basics.

Thanks.

Friday, April 1, 2016

Log every request & response in Laravel 5.1

Log every request & response in Laravel 5.1:


Today we will discuss about how to log every request & response in Laravel 5.1.

Sometimes it's useful to log some/all requests to your application. This is really convenient when you use Laravel to build your APIs.

A logging middleware might log all incoming requests to your application. In Laravel 5.1 there is a terminate method and it's call after the sending HTTP response to the browser. This way we have access to both $request and $response at the same time.

Let's take a look at how we are going to achieve this in Laravel 5.1:

Create a new middleware by type this command in terminal inside your project directory.

php artisan make:middleware LogAfterRequest

And then put below code in it(project_dir/app/Http/Middleware/LogAfterRequest.php).

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogAfterRequest {

    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);
    }

}

In this code terminate method receives $request and $response. These are objects which will give you all of the handy stuffs you probably need to log.

When we have our middleware ready, we should add it to our HTTP Kernel.

Open Kernel.php and add this line to your protected $middleware property:

\App\Http\Middleware\LogAfterRequest::class

That's it. You can additionally filter what you want to actually log, but this is the basics.

Thanks.

Friday, March 18, 2016

How to get the date of first day of previous month in php

How to get the date of  first day of previous month in php:

For getting the date of first day of previous month in php we have to use this code.


    public function getDate()
    {
        $curMonth = date('m', strtotime(date('Y-m-d')));
        $curYear = date('Y', strtotime(date('Y-m-d')));
        $occDate="$curYear-$curMonth-01";
        $forOdNextMonth= date('m', strtotime("-1 month", strtotime($occDate)));
        $forOdNextYear= date('Y', strtotime("-1 month", strtotime($occDate)));
        return "$forOdNextYear-$forOdNextMonth-01";
    }



Using this code we can get the desire result.

It will be helpful for getting the previous year even in December month which usually shows some problem for fetch.

Thanks

Friday, March 11, 2016

Create pagination using an array instead of object in Laravel

Create pagination using an array instead of object in Laravel:

In Laravel 5 we can use pagination by using simplePaginate() or paginate() functions. These functions works in case of object.

If we want to direct use this pagination in any array then we have to use the following code.

Firstly include these two lines:

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;


After that use this:


    public function indexTransaction()
    {
        $fullDetail = array();
        $user = new User();
        $txnDetail = Transaction::getAllDetail();
        foreach ($txnDetail as $k => $v) {
            $fullDetail[$k]['admin_amt'] = $v->admin_amt;
            $fullDetail[$k]['publisher_amt'] = $v->publisher_amt;
            $fullDetail[$k]['reseller_amt'] = $v->reseller_amt;
            $fullDetail[$k]['pub_sent_status'] = $v->pub_sent_status;
            $fullDetail[$k]['reseller_sent_status'] = $v->reseller_sent_status;
        }
        $finalDetail = $this->paginate($fullDetail, '20');
        return view('admin.transaction')->with('txnDetail', $finalDetail);
    }

    public function paginate($items, $perPage)
    {
        $pageStart = \Request::get('page', 1);
        $offSet = ($pageStart * $perPage) - $perPage;
        $itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);
        return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
    }



This will create a pagination in case of array.

Thanks

Friday, March 4, 2016

How to modify request data before validation in Laravel 5

Modify request data before validation in Laravel 5:

1. Replace All request data
Use replace() function to change all requested data with given data
 For example
$request contains first_name, last_name field.
and we want to create one filed name with combination of first_name and last_name


$newRequest = array('name'=>$request->first_name.$request->last_name);
$request->replace($newRequest);

now $request contains name field.

2. Modify Request field data.
Use merege() function to modify  requested data with given data
For Example
$request contains name field with null value .
and we want to change null to empty string.
$newRequest = array('name' => '');
$request->merge($newRequest);




Thanks