Wednesday, August 10, 2016

Add Cron jobs to run scheduling jobs for Laravel Developer on Ubuntu

I have faced a lot of problems to run schedule jobs in Laravel.

I am using Ubuntu 14.04 and i spent a lot of time to make it work.

Here i wrote down the basic procedure that i followed.

First 

To run scheduled jobs first you have to create a new command. To make a command go to your project directory and run following code in terminal:

 
php artisan make:console Hello

If you go to app/Console/Commands directory you'll see a new Hello.php class created.

Now change the signature variable and give a name of your newly created command

protected $signature = 'Hello';
Now change the description of the command if you like.

Now go to app/Console/kernal.php and update commands variable

protected $commands = [
    Hello::class,];
Don't forget to add namespace for Hello.php clas.
Now in handle function we want to do what ever we want. In this example we want to return 'Hi Rashed';

public function handle(){
Log::info('i was here ');}
and add use Log; at the of the Hello.php
Now Add following code in schedule function on kernal.php file.
protected function schedule(Schedule $schedule){    $schedule->command('Hello')->everyMinute();}
So you can see i am trying to run Hello command in every minutes.
Now Everything is ready and all we have to do is setting up a cron for this project.

To do so, open a terminal and type

corntab -e

Now add following line :

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Now Run your Project and check storage/logs/laravel.log file to see message.

Tuesday, August 9, 2016

Export Excel files in CSV format with UTF-8 encoding in Laravel

Case : You are given  2 dates ($dateFrom , $dateTo) and you have to find data within given range and export this data in csv format which can encode utf-8 character set.

We can divide this whole process in 2 parts.

First( Get Data)

$first_date = date('Y-m-d 00:00:00', strtotime($dateFrom));
$last_date  = date('Y-m-d 23:59:59', strtotime($dateTo));
Now get data from database: 
$admission_details_all=$this->getCompletedReportResult($first_date,$last_date);
Second(Export To Csv)
 First we have to define , which field we want to show in csv file.

So declare fields in an array as follow :

$arrColumns = array('name', 'phone_no', 'branch_name', 'course_name');

and remember this fields represents the field name you'll query from database.

The define first row of the csv file:

$arrFirstRow = array('Name', 'Phone No', 'Branch', 'Course');
$options = array(    'columns' => $arrColumns,    'firstRow' => $arrFirstRow,);
now create a function convertToCSV($admission_details_all, $options, $dateFrom ,$reportType) 
$admission_details_all is the data we get from database
$options is an array which has $arrColumns and $arrFirstRow
$dateFrom is a date time required to track when you exported your file
$reportType is required to name the exported file
now we have to call  convertToCSV function and return it.
return $csvconversionrepo->convertToCSV($admission_details_all, $options, $dateFrom ,$reportType); 
Here is the screen shot of  convertToCSV funtion