【问题标题】:`Class not found` error even though class is defined as a modal即使类被定义为模态,`Class not found` 错误
【发布时间】:2016-12-13 23:57:00
【问题描述】:

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

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

控制器运行时出现以下错误:

现在我确实有以下模态类:

<?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 found 中遇到这个错误,即使我已经定义了这个类??

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    只需在控制器类的顶部使用 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

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

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

      use App\Notice;
      

      或者使用完整的命名空间:

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

      【讨论】:

      • 我收到此错误imgur.com/k0XQdAm。我一提交表格。
      • @AlexanderSolonik 你今天问了关于这个错误的问题,我已经回答了它并在我的答案中添加了正确的代码。请检查它,它将修复您遇到的新错误。
      • @Alecey,是的,你今天的回答是对的,但现在我在提交表单时收到此错误:(
      猜你喜欢
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2015-02-12
      • 2023-04-03
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多