"类未找到"错误,即使类被定义为模态

Ale*_*nik 2 php laravel laravel-5

我的控制器中有以下方法:

public function store(Request $request){
        $data = session()->get('dmca');
        // return $data;
        $notice = Notice::open($date);
        $notice->useTemplate($request->input('template'));
        $notice->save();
        return Notice::first();
    }
Run Code Online (Sandbox Code Playgroud)

当控制器运行时,我得到以下错误:

在此输入图像描述

现在我有以下模态类:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Notice extends Model {

    protected $fillable = [
        'provider_id',
        'infringing_title',    
        'infringing_link',    
        'original_link',    
        'original_description',    
        'template',    
        'content_removed'
    ];


    public static function open(array $attributes) {
        return new static($attributes); 
    } 

    public function useTemplate($template) {
        $this->template = $template;
    }

}
Run Code Online (Sandbox Code Playgroud)

现在为什么我在laravel类中找不到这个错误,即使我已经定义了这个类?

Sau*_*ogi 5

只需use在控制器类的顶部使用关键字即可.

<?php

namespace App\Http\Controllers;

use App\Notice; // <------------------ use here
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    public function store(Request $request)
    {
        $data = session()->get('dmca');
        // return $data;
        $notice = Notice::open($date);
        $notice->useTemplate($request->input('template'));
        $notice->save();
        return Notice::first();
    }
}
Run Code Online (Sandbox Code Playgroud)

use关键字帮助php识别Notice某个命名空间中的类

希望这可以帮助!