试图在laravel发送使用命令电子邮件5.4

问题描述:

我想在laravel 5.4使用任务调度程序发送电子邮件及以下代码试图在laravel发送使用命令电子邮件5.4

我发邮件控制器

namespace App\Http\Controllers; 

use Auth; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Mail; 

# MAiler 
use App\Mail\Mailtrap; 
use App\Mail\EmailNotification; 

class MailController extends Controller 
{ 
    /** 
    * Send email 
    * @return 
    */ 
    public function index(){ 
     $user = Auth::user(); 
     Mail::to($user)->send(new EmailNotification()); 
    } 

} 

接下来,我创建了一个命令,并在那里

namespace App\Console\Commands; 

use Illuminate\Console\Command; 
use App\Http\Controllers\MailController; 

class SendNotifications extends Command 
{ 
    /** 
    * The name and signature of the console command. 
    * 
    * @var string 
    */ 
    protected $signature = 'SendNotifications:notification'; 

    /** 
    * The console command description. 
    * 
    * @var string 
    */ 
    protected $description = 'This will send email'; 

    /** 
    * Create a new command instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    /** 
    * Execute the console command. 
    * 
    * @return mixed 
    */ 
    public function handle() 
    { 
     $mail = new MailController(); 
     $mail->index(); 
    } 
} 

和在内核使用控制器

namespace App\Console; 

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\SendNotifications' 
    ]; 

    /** 
    * Define the application's command schedule. 
    * 
    * @param \Illuminate\Console\Scheduling\Schedule $schedule 
    * @return void 
    */ 
    protected function schedule(Schedule $schedule) 
    { 
     $schedule->command('SendNotifications:notification') 
       ->everyMinute(); 
    } 

    /** 
    * Register the Closure based commands for the application. 
    * 
    * @return void 
    */ 
    protected function commands() 
    { 
     require base_path('routes/console.php'); 
    } 
} 

,当我尝试使用web.php路线控制器发送电子邮件,它成功地发送电子邮件至mailtrap.io但是,当我尝试在后台发送使用此命令

php artisan SendNotification:notification

我得到这个错误

Trying to get property of non-object

我不知道为什么它被认为是成功的,因为它只是叫控制器中的电子邮件或我实现了这个错误

你能指导我吗?

因为你在不同的会议上工作。您可能已登录浏览器并为此用户创建了会话,但此会话在您的命令行中不相同。

所以在这行中出现的问题$user = Auth::user();

如果dd($user)在控制台的命令,你会发现$用户是空

而这就是为什么你得到试图获得非对象的属性错误