我正在尝试在我的laravel项目中运行脚本,当我遇到模型类找不到错误时。
我的模型类位于:
应用\ UserLoginAudit;
我这样称呼我的班级:
<?php
namespace App\Scripts;
use App\UserLoginAudit as user_login_audit;
use Illuminate\Support\Facades\DB;
class UserSignOut {
private $currentDate;
private $report;
public function __construct() {
$this->currentDate = date('Y-m-d H:i:s');
$this->doQuery();
}
private function doQuery () {
$this->report = new user_login_audit();
//die(print_r($this->report,true));
$this->report->select(
DB::raw('TIMEDIFF(NOW(),user_login_audit.last_accessed) as time_difference'),
'user.user_id',
'user_login_audit.ip_address'
)
->join(
'tutor_times.user',
'user.user_id',
'=',
'user_login_audit.user_id'
)
->whereNull('user_login_audit.time_out_type')
->where('last_accessed','<=',$this->currentDate)
->get()
->toArray();
die(print_r($this->report,true));
$this->processQuery();
}
private function processQuery () {
foreach ($this->report as $record) {
if ($record['time_difference'] > 5) {
$lookup = user_login_audit
::where('user_id','=',$record['user_id'])
->where('ip_address','=',$record['ip_address'])
->whereNull('time_out_type')
->first();
if (!empty($lookup)) {
$lookup->time_out_type = "system";
$lookup->save();
}
}
}
}
}
$obj = new UserSignOut();
我错过了什么吗?请有人帮我解决这个问题
当我运行脚本时,这是我得到的错误:
未捕获错误:类&#39; App \ UserLoginAudit&#39;在/home/public/TutorTimes/app/Scripts/user_sign_out.php:17中找不到
答案 0 :(得分:1)
执行此操作的正确方法是custom Artisan command。
答案 1 :(得分:1)
通过文件和类的名称,我可以理解您手动运行/加载脚本。 在这种情况下,您无法加载作曲家的自动加载器,它应该为您加载类。
尝试创建自定义artisan命令并使用php artisan {commandName}
运行它。
有关创建自定义工匠命令的信息,请参阅Laravel docs。
****更新****
根据您的需要,使用此命令(不要忘记注册):
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\UserLoginAudit as user_login_audit;
use Illuminate\Support\Facades\DB;
class UserSignOut extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user-sign-our';
/**
* The console command description.
*
* @var string
*/
protected $description = '...';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->report = new user_login_audit();
$this->report->select(
DB::raw('TIMEDIFF(NOW(),user_login_audit.last_accessed) as time_difference'),
'user.user_id',
'user_login_audit.ip_address'
)
->join(
'tutor_times.user',
'user.user_id',
'=',
'user_login_audit.user_id'
)
->whereNull('user_login_audit.time_out_type')
->where('last_accessed', '<=', $this->currentDate)
->get()
->toArray();
die(print_r($this->report, true));
$this->processQuery();
}
/**
*
*/
private function processQuery()
{
foreach ($this->report as $record) {
if ($record['time_difference'] > 5) {
$lookup = user_login_audit
::where('user_id', '=', $record['user_id'])
->where('ip_address', '=', $record['ip_address'])
->whereNull('time_out_type')
->first();
if ( ! empty($lookup)) {
$lookup->time_out_type = "system";
$lookup->save();
}
}
}
}
}
要运行它,请使用:
php artisan user-sign-our
答案 2 :(得分:0)
试试这个
use \App\UserLoginAudit as user_login_audit;