Laravel 使用 Schedule 每周为多个用户创建条目
Laravel Using Schedule to create entries every week for multiple users
我正在尝试自动生成多个条目,每周一早上 07:00 每个用户一个。
还需要检查条目是否存在,因为用户可以在需要时创建自己的条目,我已经使用 Requests timesheetRequest.php
验证了手动创建
Console/Commands/AddSheets.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use Carbon\Carbon;
use DB;
class AddSheets extends Command
{
protected $signature = 'add:sheets';
protected $description = 'Generate new timesheet for all active users';
public function handle()
{
/**
* get users where active
* if does not exist, create a new timesheet for each user for current week 2018-11 W/C 24-03 MS
*/
$users = User::with('profile')->where('status_id', '1')->get();
foreach ($users as $user) {
$currentweek = Carbon::now()
->year . '-' . Carbon::now('w')
->weekOfYear . ' W/C ' . Carbon::now()
->day . '-' . Carbon::now()
->month . ' ' . $user->profile->code();
DB::table('timesheets')->insert([
'name' => $currentweek,
'status_id' => 1,
'user_id' => $user->id
]);
}
$this->info('Add:Sheets Command Run successfully!');
}
}
和
Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Models\User;
use Carbon\Carbon;
use DB;
class Kernel extends ConsoleKernel
{
protected $commands = [
Commands\AddSheets::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command('add:sheets')->weekly()->mondays('07:00');
}
protected function commands()
{
require base_path('routes/console.php');
}
}
当我运行
php artisan add:sheets
我明白了
In Builder.php line 2445:
Call to undefined method Illuminate\Database\Query\Builder::code()
但我不确定这意味着什么,并且在源代码中看不到任何暗示除了第 2445 行之外的任何内容是异常的错误消息。
我现在很茫然
请帮忙
更新此变量声明:
$currentweek = Carbon::now()
->year . '-' . Carbon::now('w')
->weekOfYear . ' W/C ' . Carbon::now()
->day . '-' . Carbon::now()
->month . ' ' . $user->profile->code(); // Here
到
$currentweek = Carbon::now()->format("y-W \W/C d-m") . ' ' . $user->profile->code();
您收到的错误是因为您正在尝试使用 profile
模型的 code()
方法,但它不存在,请确保您在正确的对象上使用了该方法,如果您尝试访问 属性,请使用 $user->profile->code
,而不是像这样:
$currentweek = Carbon::now()->format("y-W \W/C d-m") . ' ' . $user->profile->code;
希望对您有所帮助。
我同意@Asur 的观点,您在 $current_week
中的 Carbon::now() 语句看起来很疯狂。在这种情况下,如果错误消息没有提供信息,我所做的是在代码的连续点添加目标 echo
、var_dump()
和 dd()
语句,使您能够查明错误所在正在发生。例如作为 foreach 循环中的第一行,您应该插入 echo 'now in foreach loop';
然后肯定在下一行之后执行 dd($current_week)
并查看它给您的内容。
刚刚注意到@Rami 也是完全正确的——访问 属性 你不使用 ()
,该语法是访问方法或查询构建器对象。该错误看起来像是试图访问不存在的查询构建器对象,因为 code
是 属性.
我正在尝试自动生成多个条目,每周一早上 07:00 每个用户一个。
还需要检查条目是否存在,因为用户可以在需要时创建自己的条目,我已经使用 Requests timesheetRequest.php
验证了手动创建Console/Commands/AddSheets.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\User;
use Carbon\Carbon;
use DB;
class AddSheets extends Command
{
protected $signature = 'add:sheets';
protected $description = 'Generate new timesheet for all active users';
public function handle()
{
/**
* get users where active
* if does not exist, create a new timesheet for each user for current week 2018-11 W/C 24-03 MS
*/
$users = User::with('profile')->where('status_id', '1')->get();
foreach ($users as $user) {
$currentweek = Carbon::now()
->year . '-' . Carbon::now('w')
->weekOfYear . ' W/C ' . Carbon::now()
->day . '-' . Carbon::now()
->month . ' ' . $user->profile->code();
DB::table('timesheets')->insert([
'name' => $currentweek,
'status_id' => 1,
'user_id' => $user->id
]);
}
$this->info('Add:Sheets Command Run successfully!');
}
}
和
Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use App\Models\User;
use Carbon\Carbon;
use DB;
class Kernel extends ConsoleKernel
{
protected $commands = [
Commands\AddSheets::class
];
protected function schedule(Schedule $schedule)
{
$schedule->command('add:sheets')->weekly()->mondays('07:00');
}
protected function commands()
{
require base_path('routes/console.php');
}
}
当我运行
php artisan add:sheets
我明白了
In Builder.php line 2445:
Call to undefined method Illuminate\Database\Query\Builder::code()
但我不确定这意味着什么,并且在源代码中看不到任何暗示除了第 2445 行之外的任何内容是异常的错误消息。
我现在很茫然
请帮忙
更新此变量声明:
$currentweek = Carbon::now()
->year . '-' . Carbon::now('w')
->weekOfYear . ' W/C ' . Carbon::now()
->day . '-' . Carbon::now()
->month . ' ' . $user->profile->code(); // Here
到
$currentweek = Carbon::now()->format("y-W \W/C d-m") . ' ' . $user->profile->code();
您收到的错误是因为您正在尝试使用 profile
模型的 code()
方法,但它不存在,请确保您在正确的对象上使用了该方法,如果您尝试访问 属性,请使用 $user->profile->code
,而不是像这样:
$currentweek = Carbon::now()->format("y-W \W/C d-m") . ' ' . $user->profile->code;
希望对您有所帮助。
我同意@Asur 的观点,您在 $current_week
中的 Carbon::now() 语句看起来很疯狂。在这种情况下,如果错误消息没有提供信息,我所做的是在代码的连续点添加目标 echo
、var_dump()
和 dd()
语句,使您能够查明错误所在正在发生。例如作为 foreach 循环中的第一行,您应该插入 echo 'now in foreach loop';
然后肯定在下一行之后执行 dd($current_week)
并查看它给您的内容。
刚刚注意到@Rami 也是完全正确的——访问 属性 你不使用 ()
,该语法是访问方法或查询构建器对象。该错误看起来像是试图访问不存在的查询构建器对象,因为 code
是 属性.