FatalThrowableError Class 'App\Models\Patient' not found in laravel 5.4

时间:2018-05-24 07:54:31

标签: php laravel

I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.

In Model Patient:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Patient extends Model
{
    //
    public function getPurchaseOrder(){
        return "Hello World";        
    }
}

In Controller PatientController:

namespace App\Http\Controllers;
class PatientController extends Controller
{
  protected $patientModel;

public function __construct(Request $request, PatientInterface $patient)
    {
        parent::__construct($request);        
        $this->patientModel = new \App\Patient();
    }

   public function test(){
     echo $this->patientModel->getPurchaseOrder();
  }

}

It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:

FatalThrowableError Class 'App\Models\Patient' not found

Any help will be appreciated.

1 个答案:

答案 0 :(得分:1)

当您将一个类移动到另一个目录时,要由作曲家使用PSR-4 standard加载它,您还必须更新类的名称空间以匹配。

namespace App\Models;

此外,当您运行make命令时,您可以在其中包含一个名称空间,以自动将其放在具有正确名称空间的目录中:

php artisan make:model Models\\Patient