首页 > 解决方案 > 即使我已导入,也找不到类“App\Models\Registration”

问题描述

找不到类“App\Models\Registration”,我有导入注册类

我正在尝试使用相应的主题数组保存 student_id 当我转储 dd($request->all()) 时,我得到了例外结果

"student_id" => "1"
"subjects" => array:2 [▼
 0 => "1"
 1 => "2"
]

但是当我尝试保存到数据库时出现异常

这是我的注册计划

 Schema::create('registrations', function (Blueprint $table) {
       $table->bigIncrements('id');
        $table->unsignedBigInteger('student_id')->index();
        $table->string('subjects');

        $table->foreign('student_id')->references('id')->on('students');

        $table->timestamps();;
    });

这是我的注册模型

   class Registration extends Model
   {
    protected $table = 'registrations';

    protected $fillable = ['student_id','subjects'];

    protected $cast = [
        'student_id'    => 'Integer',
        'subjects'      => 'array',
    ];

  public function student(){
    $this->belongsTo(Student::class);
  }

  public function subjects()
  {
    $this->hasMany(Subject::class);
  }

}

我正在使用复选框数组来获取主题

 <input class="form-check-input" name="subjects[]" value="{{$subject->id}}" type="checkbox">

这是注册控制器代码,我已经导入了注册模型

namespace App\Http\Controllers\Admin;

  use Illuminate\Http\Request;
  use App\Models\Registration;
  use App\Http\Controllers\BaseController;

  class RegistrationController extends BaseController
  {
    public function store(Request $request)
    {
        $registration = Registration::create(request()->validate([
        'student_id' => 'required|integer',
        'subjects' => 'required',
        'subjects.*'=> 'accepted',
    ]));

 }

我想用主题数组保存 student_id

学生科目

1 [2,4,5]enter code here

标签: laravel

解决方案


我认为您需要添加一个命名空间。

<?php

namespace App\Models;

class Registration extends Model { ... }

?>

此外,您的模型必须存储在App/Models/ Registration.php目录中。


推荐阅读