繁体   English   中英

无法识别的类 - laravel Eloquent 模型

[英]Class not recognized - laravel Eloquent Model

我正在尝试使用 Eloquent 模型更新我的数据库,但无法识别该类。

首先,我使用迁移创建了我的表,并且工作正常。下面是代码 use Illuminate\\Database\\Schema\\Blueprint; 使用 Illuminate\\Database\\Migrations\\Migration;

class CreatePaintings extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('paintings',function($thepainting){
            $thepainting->increments('id');
            $thepainting->string('title');
            $thepainting->string('artist');
            $thepainting->integer('year');
            $thepainting->timestamps();

    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('paintings');
}
}

接下来,我使用模型创建了一个“paint”类。 但请注意,最新版本的 laravel 没有明确指定模型文件夹。 所以当我在命令提示符下运行下面的代码时,我能够创建油漆类

php artisan make:model paint

最后,我尝试使用下面的代码通过 routes.php 更新创建的表、绘画。

Route::get('/', function() 
{
$paintings = new Paint;
$paintings->title = 'Emmanuel';
$paintings->artist = 'D. DoRight';
$painitngs->year = 2014;
$paintings->save();

return view('trynn');

});


 Route::get('about/directions', function() 
 {
 return "Direction content goes here";
  });

 Route::get('about/{theSubject}', function($theSubject) 
  {  
  return $theSubject. " content goes here";

  });

请我是laravel的新手,所以我会很感激任何帮助解决这个问题。 我目前被困。 免得我忘记了,错误信息如下所示


哎呀,看起来出事了。

route.php 第 18 行中的 1/1 FatalErrorException:找不到“Paint”类

在 routes.php 第 18 行

啊,这是一个简单的错误。 如果不是全部,您能否将文件的全部内容复制粘贴进去。

同样的错误是因为您没有从自动加载器中引入类。 在 Laravel 中,一切都是命名空间的。 在你的画图模型中,在顶部你会看到namespace App ,如果你将画图模型移动到一个模型目录中,你需要命名空间类App\\Models;

为了修复这个错误,在routes.php文件的顶部,写use App\\Paint; . 但是您真的应该将该路由发送到控制器以将代码与路由分开。

如果这有帮助,请告诉我。

找不到类 Pain 是正常的,请检查您在此处完成的操作:

php artisan make:model paint

并在您的路线文件中:

$paintings = new Paint;

尝试将您的模型类名称更正为此Paint ,并且当您要实例化它时,请不要忘记默认情况下生成的模型类的命名空间,因此您的路由文件将如下所示

Route::get('/', function() 
{
      $paintings = new App\Paint;
      $paintings->title = 'Emmanuel';
      $paintings->artist = 'D. DoRight';
      $painitngs->year = 2014;
      $paintings->save();
      return view('trynn');

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM