getting error like:- [Symfony\Component\Console\Exception\RuntimeException] Too many arguments - laravel

This is my UserCreateCommand.php file when command:- php artisan user-create username password getting error like:- [Symfony\Component\Console\Exception\RuntimeException] Too many arguments.
class UserCreateCommand extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'user-create';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create user with password imediately';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
//
$username=$this->argument('username');
$password=$this->argument('password');
$user= new UserDemo();
$user->username=$username;
$user->password=Hash::make($password);
$user->save();
$this->info('user has been cretaed');
}
}

public function handle()
{
//
$username=$this->ask('username');
$password=$this->secret('password');
$user= new UserDemo();
$user->username=$username;
$user->password=Hash::make($password);
$user->save();
$this->info('user has been cretaed');
}
working fine when used command:- php artisan user-create

Related

how to send notification in App that a date is approaching with scheduler in Laravel

I'm trying to send notification to user(i have only one user ,it's a dashboard)
in the App if the start_date is approaching ,so i tried to use task scheduler but i don't know how to use notification with task scheduler , should I create a table that save the notification in the database ?
So this is what i did:
class CongeNotification extends Notification
{
use Queueable;
public $conge;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(Conge $conge)
{
$this->conge = $conge;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database'];
}
public function toArray($notifiable)
{
return [
'conge_id' => $this->conge->id
];
}
and this is my command:
class NotifyUsers extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'users:notify';
/**
* The console command description.
*
* #var string
*/
protected $description = 'conge notification added';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
//$conge =Conge::all();
$now = Carbon::now();
$conge =Conge::where('start_date','<',$now->addDays(7))->get() ->each(function ($conge) {
DB::table('notification')->insert($conge->id);
});
}
`please any help`
i tried to create in handle a notification that notify the user a 7 day before the start_date

cron job with laravel to send emails to user

please help me i'm trying to make a cron job on my server to send email to user
i created a command like that :
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'contract:end';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send an email to user about the end of a contract';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$contracts = Contract::where('end_date', Carbon::parse(today())->toDateString())->get();
foreach ($contracts as $contract) {
Mail::to(Setting::first()->value('email'))->send(new ContractEmail($contract));
}
}
and in my kernel.php i added the following code :
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
//
Commands\ContractEnd::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('contract:end')
->everyMinute();
}
/**
* Register the commands for the application.
*
* #return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
and on my server i run the following command to run the cron job :
cd /archive/artisan && schedule:run >> /dev/null 2>&1
but it didn't send any email , can anyone help me as it's my first time using it ?
Because your command in your server is wrong you need to specify the correct path which will contain/home/your username/your website folder/artisan like this
/usr/local/bin/php /home/your user name which you can get in from job page/archive/artisan schedule:run 1>> /dev/null 2>&1

my cron job not working well in laravel it work only once

i am working on laravel crone job it work only one time my code execute only one time i am a beginer
protected function schedule(Schedule $schedule)
{
$schedule->command('list:users')->everyMinute();
}
class ListUsers extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'list:users';
/**
* The console command description.
*
* #var string
*/
protected $description = 'corn job command';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
}
I am also try this code
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
echo "pakistan\n";
$totalUsers = \DB::table('users')->whereMonth('created_at','04')->count();
Mail::to('qasim#gmail.com')->send(new SendMailable($totalUsers));
echo "pakistan";
})->everyMinute();
}
i want user recieve email after every minute
i recieve mail once but not every minute

Laravel 5.0 : Task scheduling

I am using Laravel 5.0.*, Now I want to implement cron job in my application.
I searched a lot but I get nothing, all the example and video tutorials are related to 5.1 and 5.3 even I search in laravel.com for task scheduling under 5.0 version but it showing nothing
Reference video link : video link
And after using above video reference, I am getting below error in terminal.
[2016-11-02 08:47:06] local.ERROR: exception 'InvalidArgumentException' with message 'There are no commands defined in the "cron" namespace.' in D:\harendar\htdocs\nkbuild\vendor\symfony\console\Symfony\Component\Console\Application.php:501
/app/Console/Kernel.php
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel {
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
'App\Console\Commands\Inspire',
'App\Console\Commands\LogDemo',
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')
->hourly();
$schedule->command('log:demo')->emailOutputTo('harendar#solutionavenues.com');
}
}
/app/Console/Commands/LogDemo.php
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class LogDemo extends Command {
/**
* The console command name.
*
* #var string
*/
protected $name = 'log:demo';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Log Demo command.';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function fire()
{
\log::info('I was here # ', \Carbon\Carbon::now());
}
/**
* Get the console command arguments.
*
* #return array
*/
protected function getArguments()
{
return [
['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
/**
* Get the console command options.
*
* #return array
*/
protected function getOptions()
{
return [
['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}
Hello First you need to run command : php artisan make:console test(command name).
Then you find one test.php command created under app/Console/Commands/test.php .this looks like:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class test extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'test';
/**
* 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()
{
//
}
}

How to pass in data into the Artisan handle command - Laravel 5.2

I would I pass data into my handle method I made in my Artisan Command?
This is what I have so far:
( If I DD something in the handle method, it works, so its linking with the Artisan Command )
Controller:
public function update(Request $request, $slug) {
// Get the user assocated with Listing, and also get the slug of listing.
$listing = $request->user()->listings()->where('slug', $slug)->first();
// Flash success message after every update
Message::set('Alright!', 'Your changes were saved successfully.');
Artisan::call('emails:sendIfGuideUpdated');
// More code here...
}
My Artisan Command that I made to handle sending the emails:
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'emails:sendIfGuideUpdated';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Send out an email every minute to users that have favored a guide when that Guide updates their Listing';
public $listing;
/**
* Create a new command instance.
*
* #return void
*/
public function __construct(Listing $listing)
{
$this->listing = $listing;
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
// Get the data from the Guide Listing that was updated
$name = $this->listing->name;
dd($name);
// Send an email to the Admin notifying that a comment was made under a blog post.
}
}
And this is my Kernal file that will handle when the emails get sent out:
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
Commands\Inspire::class,
Commands\SendEmails::class,
];
/**
* Define the application's command schedule.
*
* #param \Illuminate\Console\Scheduling\Schedule $schedule
* #return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('emails:sendIfGuideUpdated')->everyMinute();
}
}
Im having trouble passing in the Listing information into to handle method. What is the correct way of doing this?
Try to update the signature of the command like this : protected $signature = 'emails:sendIfGuideUpdated {listing}';
And leave the __construct blank.
Then your handel method will looks like this :
public function handle()
{
// Get the data from the Guide Listing that was updated
$listing = $this->argument('listing')
$name = $listing->name;
dd($name);
// Send an email to the Admin notifying that a comment was made under a blog post.
}
Also the call of this command will be :
Artisan::call('emails:sendIfGuideUpdated', ['listing' => $listing]);

Resources