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

时间:2016-12-13 15:48:04

标签: 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();
    }

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

enter image description here

现在我有以下模态类:

<?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;
    }

}

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

2 个答案:

答案 0 :(得分: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();
    }
}
  

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

希望这有帮助!

答案 1 :(得分:4)

将此行添加到控制器的顶部:

use App\Notice;

或使用完整命名空间:

$notice = \App\Notice::open($date);