i have mysql table which is having earnings records with date. i want to get total earning on daily ,weekly and monthly basis.
I tried to get these records on basis of date but is confusing.
i want to get these reports on daily , weekly and monthly basis.
You can use the existing Laravel cron job scheduling to fulfill your specific requirement.
Please refer following Laravels official documentation
https://laravel.com/docs/5.8/scheduling#scheduling-queued-jobs
I'll show a simple example to get an idea about this configuration.
php artisan make:command DailyUpdate
above command will create a class component in the following location
app/Console/Commands directory.
Generated class file will be similar to below,
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class DailyUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'command:name';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
}
}
After that change the following according to your requirement.
protected $signature = 'command:name';
protected $description = ‘Command description’;
Inside the handle() you can place the implementation that you need to repeat on a scheduled basis.
public function handle() {
// implementation
}
After all the changes, you need to register that command.
In app/console/kernal.php change the configuration as follows,
protected $commands = [
Commands\DailyUpdate::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command('hour:update')
->daily();
}
Related
So, I want to make a function which will keep updating my product inventory and I need the function to be running continuously as long as the server or application would be running.....
Thanks In Advance!!!
My Expectations:
while (true)
{
$product->quantity += $updated
// This would be running continuously
}
Ok, after some help I got the solution to use laravel commands with task scheduling with the least available running of every one minute.
First, Make a new command:
php artisan make:command ProductQuantityUpdate // Command Name
Second, Locate the ProductQuantityUpdate.php at App\Console\Commands\ProductQuantityUpdate.php and type your desired function in the handle():
Note: You can also edit the command name & description through the:
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'productquantity:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update Product Quantity!';
Full Code [App\Console\Commands\ProductQuantityUpdate.php]:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ProductQuantityUpdate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'productquantity:update';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Update Product Quantity!';
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$pro = \App\Models\Product::get();
foreach($pro as $product)
{
$qty = 0;
$selecteds = \App\Models\AttributeValue::where('product_id', $product->id)->get();
if($selecteds->count() > 0)
{
foreach($selecteds as $select)
{
$qty += $select->quantity;
}
$product->quantity = $qty;
$product->save();
}
}
$this->info('Product Quantity Updated!');
return Command::SUCCESS;
}
}
Third, Go to App\Console\Kernel.php and add the command, and schedule function:
protected $commands = [
Commands\ProductQuantityUpdate::class,
];
protected function schedule(Schedule $schedule)
{
$schedule->command('productquantity:update')->everyMinute();
// Check the documentation for available timings!
}
To check your command, type this in the console:
php artisan list
To check your schedule, type this in the console:
php artisan schedule:list
Documentation that also helped me : Couldways documentation
I have written code for cron command as below in lumen (micro framwork of laravel)
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\EmailDump;
use DB;
/**
* dumpEmails Class
*
* This cron is to dump emails with cron use
*
* #author Hetal Gohel <hetal.gohel#brainvire.com>
*
*/
class dumpEmails extends Command {
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'insert:emails';
/**
* The console command description.
*
* #var string
*/
protected $description = 'This cron is to dump emails with cron use';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
echo "1";die;
}
}
in kernel file defined as below,
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Laravel\Lumen\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\Laravelista\LumenVendorPublish\VendorPublishCommand::class,
'\App\Console\Commands\dumpEmails',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
// protected function schedule(Schedule $schedule)
// {
// return $schedule;
// }
}
I have fired command from console as below,
php artisan insert:emails
while run this command getting error as below,
[Symfony\Component\Console\Exception\CommandNotFoundException] ←[39;49m
←[37;41m There are no commands defined in the "insert" namespace.
please help me to resolve this issue.thank you.
Please remove __construct and keep just the handle method.
Also, when you are listing under $commands at Kernel you need to specify the class.
So your
`\App\Console\Commands\dumpEmails`
becomes
DumpEmails::class
A few extra tips:
Class names are capitalised; (dumpEmails -> DumpEmails)
Add the { to a new line;
This
class dumpEmails extends Command {
should be
class DumpEmails extends Command
{
I also recommend checking out about PSR-x standards. I left one of the blogs I think might help you kick start with them, but go further! :)
Last, but not least, do not forget Command Parent already lets you use its command line potential. So if you wish to output and debug, you can do so by using:
$this->info('Your message to inform');
$this->error('Your error message');
I am doing ERP project in cloud base & I am using one source for multiple customers and using different database.
I want to run script every day at 00:00 with different time-zone
Here what i tried......
This is my Kernel class
protected function schedule(Schedule $schedule)
{
$schedule->command('attendance:null')->hourly();
}
This is my attendance:null command class
class AttendanceInsertNull extends Command{
protected $signature = 'attendance:null';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return \App\Console\Commands\AttendanceInsertNull
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$locations = Location::get();
foreach ($locations as $location) {
$schedule1 = new Schedule();
$schedule1->call(function(){
$timeZone = $location->timezone;
$this->info('RUN');
})->at('00:00')->timezone($timeZone);
}
}
}
What i am doing is, Check with different timezone in location loop.
please anyone can help to solve this issue for me.
As part of my Laravel 5.2 application, I would like to define a custom command for artisan, but my command doesn't appear in artisan list.
1). I created the command skeleton: artisan make:console --command=process:emails
2). I added a bit of test code to the handle() method of the new class:
<?php
namespace App\Console\Commands;
use App\CommunicationsQueue;
use Illuminate\Console\Command;
class ProcessEmailQueueCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'process:email';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send all currently pending emails in the queue';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
CommunicationsQueue::where('status', 'PENDING')->update(['status'=>'TEST']);
$this->info('The mails queue was successfully processed.');
}
}
3). Then, I registerred the command in app/Console/Kernel.php:
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
What am I missing here? I'm sure it's something incredibly simple, but I'm not seeing it.
in app/Console/Kernel.php try with the following code
protected $commands = [
'App\Console\Commands\ProcessEmailQueueCommand',
];
On Kernel.php, try include the full name, which should be:
protected $commands = [
\App\Console\Commands\ProcessEmailQueueCommand::class,
];
I am new in laravel. I was trying to make a custom artisan command for creating tables in my test project.I followed this link but my command was not on the artisan list.Event I tried same example given in that link but its also not worked. I dont know why its happening.
I DID THIS:
1) Run this command php artisan make:console SendEmails
2) Put complete class code in app/Console/Commands/SendEmails.php file
<?php
namespace App\Console\Commands;
use App\User;
use App\DripEmailer;
use Illuminate\Console\Command;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'email:send {user}';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send drip e-mails to a user';
/**
* The drip e-mail service.
*
* #var DripEmailer
*/
protected $drip;
/**
* Create a new command instance.
*
* #param DripEmailer $drip
* #return void
*/
public function __construct(DripEmailer $drip)
{
parent::__construct();
$this->drip = $drip;
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->drip->send(User::find($this->argument('user')));
}
}
Please help me let me know what I am doing wrong.
You just forgot to Register your command.
This part: https://laravel.com/docs/5.1/artisan#registering-commands.
Open app/Console/Kernel.php and add you command class in $commands array.
like this :
protected $commands = [
Commands\SendEmails::class
];
That's it.