i'm using laravel6, and voyager, i'm creating artisan command for installation use cmd following php artisan make: command EcommerceInstall
, I recopy DataTypesTableSeeder from database/seeder and I rename it by DataTypesTableSeederCustom, I modify my file which I wrote EcommerceInstall.php but it gives me error class [DataTypesTableSeederCustom] does not exist.
i think i forget import the class but i don't know where .
EcommerceInstall.php
public function handle()
{
if ($this->confirm('this well delete all you current data and install the dummy default data, Are you sure ?')) {
File::deleteDirectory(public_path('storage/products/dummy'));
$this->callSilent('storage:link');
$copySuccess = File::copyDirectory(public_path('img/products'),public_path('storage/products/dummy'));
if($copySuccess){
$this->info('images succefully copied to storage folder');
}
$this->call('migrate:fresh', [
'--seed' => true,
]);
$this->call('db:seed', [
'--class' => 'DataTypesTableSeederCustom'
]);
$this->info('Dummy data installed');
}
I'm trying to setup a cron job to run a console command every minute.
class RescanCommand extends Command {
public function sendMail() {
$email = new Email();
// Sample SMTP configuration.
Email::setConfigTransport('mailtrap', [
'host' => 'smtp.mailtrap.io',
'port' => 25,
'username' => 'username',
'password' => 'pass',
'className' => 'Smtp'
]);
$email->setFrom(['test#test.com' => 'CSV file'])
->setTo('test#test.com')
->setSubject('CSV Link File')
->send('Please find attached a copy of the links');
}
public function execute(Arguments $args, ConsoleIo $io) {
$this->sendMail();
}
}
I can run the above code from command line by
bin/cake rescan execute
and I receive the test email, but after creating a cron job by editing cron tabs like
crontab -e
and inside the file by writing
*/1 * * * * cd /Applications/MAMP/htdocs/music && bin/cake Rescan execute
nothing happens, I was expecting to receive emails every minute,
can anyone please help find out what I'm doing wrong.
Thanks
I am using davibennun/laravel-push-notification package for sending notification to device. But I want to send notification to multiple users for that i want to use laravel queue. But i am new to laravel that's why don't know how to use queue with notification.
I have created the migrate the queue table and have created the job by
php artisan make:job SendPushNotification command.
After running following command
php artisan make:job SendPushNotification
From your controller
$user = User::all();
$other_data = array('message' => 'This is message');
SendPushNotification::dispatch($user, $other_data);
In app\Jobs\SendPushNotification.php
protected $contacts;
protected $sms_data;
public function __construct($users, $other_data)
{
//
$this->users = $users;
$this->other_data = $other_data;
}
public function handle()
{
$users = $this->users;
$other_data = $this->other_data;
foreach($users as $key => $value){
// You code
}
Run following command
php artisan queue:work
laravel Console command...
public function handle()
{
$stores_id = $this->stores->where('status', '=', 'Active')->orderBy('code')->get(['code'])->toarray();
//-------------error i think
$stockHolddata = DB::connection('oracle')->table('mmpl.V_STOCK_ONHAND')
->whereIn('ADMSITE_CODE',$stores_id)
->where('QTY','!=','0')
->get(['BARCODE','ADMSITE_CODE','QTY'])->toJson();
//----------------------
$jsondata = json_decode($stockHolddata,true);
if(!empty($jsondata[0])){
DB::table('v_stock_onhand')->truncate();
}
foreach ($jsondata as $val){
VStockOnhand::create($val);
}
}
VStockOnhand is a laravel mysql model.....
it runs fine if I directly run php artisan InsertOracle:stock in terminal
it won't work on cron job schedule
kernel.php
protected $commands = [
'App\Console\Commands\InsertOraclestock',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('InsertOracle:stock')->dailyAt('23:00')->withoutOverlapping();
}
cron job code on crontab -e
* * * * * /usr/bin/php7.0 /var/www/rep/qa/artisan schedule:run >> /var/www/rep/qa/example.txt
I have controller like this
public function store(Request $request)
{
Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}
Show me error
There are no commands defined in the "php artisan infyom" namespace.
When I run this command in CMD it work correctly
You need to remove php artisan part and put parameters into an array to make it work:
public function store(Request $request)
{
Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}
https://laravel.com/docs/5.2/artisan#calling-commands-via-code
If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clear In route file that would be:
Route::get('clear_cache', function () {
\Artisan::call('cache:clear');
dd("Cache is cleared");
});
To run this command from browser just go to your's project route and to clear_cache. Example:
http://project_route/clear_cache
Apart from within another command, I am not really sure I can think of a good reason to do this. But if you really want to call a Laravel command from a controller (or model, etc.) then you can use Artisan::call()
Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
One interesting feature that I wasn't aware of until I just Googled this to get the right syntax is Artisan::queue(), which will process the command in the background (by your queue workers):
Route::get('/foo', function () {
Artisan::queue('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});
If you are calling a command from within another command you don't have to use the Artisan::call method - you can just do something like this:
public function handle()
{
$this->call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
}
Source: https://webdevetc.com/programming-tricks/laravel/general-laravel/how-to-run-an-artisan-command-from-a-controller/
Remove php artisan part and try:
Route::get('/run', function () {
Artisan::call("migrate");
});
Command Job,
Path : {project-path}/app/Console/Commands/RangeDatePaymentsConsoleCommand.php
This is the Job that runs with the artisan command.
class RangeDatePaymentsConsoleCommand extends Command {
protected $signature = 'batch:abc {startDate} {endDate}';
...
}
web.php,
Path : {project-path}/routes/web.php
web.php manage all the requests and route to relevant Controller and can have multiple routes for multiple controllers and multiple functions within the same controller.
$router->group(['prefix' => 'command'], function () use ($router) {
Route::get('abc/start/{startDate}/end/{endDate}', 'CommandController#abc');
});
CommandController.php,
Path : {project-path}/app/Http/Controllers/CommandController.php
This Controller is created for handle artisan commands and name can be vary but should be same to web.php Controller name and the function name.
class CommandController extends Controller {
public function abc(string $startDate, string $endDate) {
$startDate = urldecode($startDate);
$endDate = urldecode($endDate);
$exitCode = Artisan::call('batch:abc',
[
'startDate' => $startDate,
'endDate' => $endDate
]
);
return 'Command Completed Successfully. ';
}
Request : http://127.0.0.1:8000/command/abc/start/2020-01-01 00:00:00/end/2020-06-30 23:59:59
It's can be access through the web browser or Postman after startup the server. Run this command to start the php server at {project-path}
php -S 127.0.0.1:8080 public/index.php
Method #1: Using route
Route::get('run-it', function () {
(new \App\Console\Commands\ThisIsMyCommand())->handle();
});
Method #2: Using command line
php artisan command:this_is_my_command