即使已将其导入,也找不到“ App \ Models \ Registration”类

时间:2019-09-26 16:30:29

标签: laravel

找不到类“ 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

2 个答案:

答案 0 :(得分:0)

我认为您在App目录中创建了注册模型...请检查您的目录。然后使用

use App\Registration;

如果位于App \ Models目录中,则在注册模型中,

<?php namespace App\Models;
 use Illuminate\Database\Eloquent\Model; 
class Registration extends Model { }

答案 1 :(得分:0)

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

<?php

namespace App\Models;

class Registration extends Model { ... }

?>

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