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

Friday, February 26, 2016

Delete a file in Laravel 5.1

Delete file in Laravel 5.1:

Today we will discuss how to delete a file in Laravel 5.1

You know you could use PHP's unlink() method, but want to do it the Laravel way then use the File::delete() method.

<?php

namespace App\Http\Controllers;

use File;

class TestController extends Controller
{

 public function deleteFile()
 {
  // Delete a single file
  File::delete($filename);

  // Delete multiple files
  File::delete($file1, $file2, $file3);

  // Delete an array of files
  $files = array($file1, $file2);
  File::delete($files);
 }

}
?>


Errors are quietly ignored.

If there's a problem deleting the file, any error is silently ignored. If it's important that a file was deleted, check it's existence after deleting it with File::exists().

Thanks.

Friday, February 19, 2016

How to compress image in php

Compress image in php:

For compressing an image in php we have to use this code for this:


<?php
    function compress($source, $destination, $quality) {
        $info = getimagesize($source);
        if ($info['mime'] == 'image/jpeg')
            $image = imagecreatefromjpeg($source);
        elseif ($info['mime'] == 'image/gif')
            $image = imagecreatefromgif($source);
        elseif ($info['mime'] == 'image/png')
            $image = imagecreatefrompng($source);
        $res=imagejpeg($image, $destination, $quality);
        return $res;
    }
   compress('image.jpg','img/image.jpg',80);
?>



Here are the $source is the path of image and $destination is the path where we have to save the image after editing.

Thanks

Friday, February 12, 2016

How to generate a Certificate Signing Request (CSR) - Apache 2.x

Generate Certificate Signing Request in Apache:

Follow these instructions to generate a certificate signing request (CSR) for your Apache Web server. When you have completed generating your CSR, cut/copy and paste it into the CSR field on the SSL certificate-request page.
To Generate a Certificate Signing Request for Apache 2.x
1. Log in to your server's terminal (SSH).
    At the prompt, type the following command:


    openssl req -new -newkey rsa:2048 -nodes -keyout yourdomain.key -out yourdomain.csr
    Replace yourdomain with the domain name you're securing. For example, if your domain name is       coolexample.com, you would type coolexample.key and coolexample.csr.
   
2. Enter the requested information:
     2.1.  Common Name: The fully-qualified domain name, or URL, you're securing.
        If you are requesting a Wildcard certificate, add an asterisk (*) to the left of the common name where you want the wildcard, for example *.coolexample.com.
     2.2.  Organization: The legally-registered name for your business. If you are enrolling as an individual, enter the certificate requestor's name.
     2.3.  Organization Unit: If applicable, enter the DBA (doing business as) name.
     2.4.  City or Locality: Name of the city where your organization is registered/located. Do not abbreviate.
     2.5  State or Province: Name of the state or province where your organization is located. Do not abbreviate.
     2.6. Country: The two-letter International Organization for Standardization (ISO) format country code for where your organization is legally registered.
        If you do not want to enter a password for this SSL, you can leave the Passphrase field blank. However, please understand there might be additional risks.

3. Open the CSR in a text editor and copy all of the text.
    Paste the full CSR into the SSL enrollment form in your account.

Thanks

Friday, February 5, 2016

How to use CORS requests in Laravel 5 with middleware

CORS requests in Laravel 5 with middleware:

For this we have to firstly create a middleware with name CORS and write the following code in it.

<?php

namespace App\Http\Middleware;

use Closure;


class CORS
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        header("Access-Control-Allow-Origin: *");

        // ALLOW OPTIONS METHOD
        $headers = [
            'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
            'Access-Control-Allow-Headers'=> 'Content-Type, X-Auth-Token, Origin'
        ];
        if($request->getMethod() == "OPTIONS") {
            // The client-side application can set only headers allowed in Access-Control-Allow-Headers
            return Response::make('OK', 200, $headers);
        }

        $response = $next($request);
        foreach($headers as $key => $value)
            $response->header($key, $value);
        return $response;
    }
}


Now go to the app/Http/kernel.php & add this line in $routeMiddleware

'cors' => \App\Http\Middleware\CORS::class,

Now in route.php we can use is like this



Route::group(['middleware' => 'cors'], function () {
    get('/script', 'ScriptController@getDetail');
    get('/response', 'ScriptController@postDetail');
});


Thanks