2017-07-05 33 views
0

我想在我的laravel項目中運行腳本時,遇到模型類未找到錯誤的困難。未捕獲的錯誤:類未找到 - Laravel

我的模特級別位於:

App \ 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(); 

我錯過了什麼東西出來?請有人可以幫助我這個問題

當我運行該腳本,這是錯誤我得到:

未捕獲的錯誤:類「應用程序\ UserLoginAudit」在/ home /公/ TutorTimes /應用/腳本未找到/user_sign_out.php:17

+1

你如何運行腳本?如果你直接運行它,自動加載器將不會被註冊,並且PHP將無法找到類 – Idob

+0

Sudo php php文件名 –

+0

所以自動加載將不起作用,在我的解決方案下采取戰利品 – Idob

回答

1

通過您的文件和類的名稱,我可以理解您手動運行/加載腳本。 在這種情況下,您將不會加載應該爲您加載類的作曲家的自動加載器。

嘗試創建自定義工匠命令並使用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 
+0

需要'../../vendor/autoload.php'; –

+0

這有什麼區別?由於我不太確定如何去使用定製手工命令? –

+0

這是在Laravel中編寫自定義腳本的正確方法。您可以添加require行作爲@ jomin_george94說的,但您未來可能會面臨更多相關的錯誤 – Idob

0

試試這個

use \App\UserLoginAudit as user_login_audit; 
+0

我試過了但同樣的錯誤 –

+0

@ jomin_george94 UserLoginAudi的命名空間是什麼? –

+0

這就是它:namespace App; –

1

是啊,這是行不通的。要使用Laravel類,您需要Laravel的自動加載器。

這樣做的正確方法是custom Artisan command

+0

(你不應該使用'sudo',除非你**需要**,這個腳本根本不需要。) – ceejayoz

相關問題