laravel cron job无法保存到DB

时间:2017-11-17 18:04:42

标签: laravel

我在Laravel 5.2版中遇到问题 - 无法将数据保存到DB。 我在内核中创建了命令send:birthday:

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 = [
        Commands\Mtv::class,
        Commands\HappyBirthday::class,

    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {

        $schedule->command('sms:birthday')->cron('* * * * *'); 


    }

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

然后上课HappyBirthday.php jn控制台/命令

<?php

namespace App\Console\Commands;
namespace App\Http\Controllers;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;


use App\Patients as PModel;
use App\Visit;
use App\Vizit;
use App\History;
use App\ViewHelper;
use App\Patients;
use DB;
use Mail;






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

            /**
             * 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()
        {



     $users=DB::select('SELECT * FROM patients where id=17'); 
     foreach( $users as $user ) {
        Mail::raw(' '.$user->id.' reached 18 ', function ($message) {
        $message->from('admin@site.com', 'admin@site.com');
        $message->to('memail@gmail.com')->subject('birthday');

        }); 
     }



  DB::table('users')->insert([
  ['status' => 'send']

  ]);    


        }

几乎所有东西都能很好地发送邮件 - 每天发送给管理员的电子邮件(例如每分钟),但我不能在用户表中插入任何行。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

也许您在表格中有其他必填字段,而不仅仅是&#34;状态&#34;。 您可以添加all,并输入foreach以使用变量:

DB::table('users')->insert([
    'user_id' => $user->id,
    'status' => 'send'
]);