php - `Class not found` 错误,即使类被定义为模态

标签 php laravel laravel-5

我的 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();
    }

当 Controller 运行时,我收到以下错误:

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 class not find 中收到此错误,即使我定义了这个类??

最佳答案

只需在 Controller 类顶部使用 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();
    }
}

The use keyword helps php to recognize the class Notice within a certain namespace

希望这有帮助!

关于php - `Class not found` 错误,即使类被定义为模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41125147/

相关文章:

javascript - 使用 ajax 在 bootstrap carousel 上附加数据

使用 JWT Auth 时 Laravel 5.3 节流登录

php - Laravel/Nginx : 403 forbidden on middleware routes

php - 从外键检索数据,一对一关系。拉维尔

mysql - Laravel 中加入 Query 以避免举报滥用用户

php - 字符串中的引号

php - Laravel如何配置发件人姓名而不是电子邮件地址

php - 在网络上显示 iPhone 照片时尊重 EXIF 方向

php5-fpm 不适用于 ubuntu 10.10 上的 php-apc

php - 在 artisan 中重写的核心类不起作用