简体   繁体   English

无法识别的类 - laravel Eloquent 模型

[英]Class not recognized - laravel Eloquent Model

I am trying to update my database using Eloquent model but the class is not recognized.我正在尝试使用 Eloquent 模型更新我的数据库,但无法识别该类。

First I created my table using migration and that worked fine.. Below is the code use Illuminate\\Database\\Schema\\Blueprint;首先,我使用迁移创建了我的表,并且工作正常。下面是代码 use Illuminate\\Database\\Schema\\Blueprint; use Illuminate\\Database\\Migrations\\Migration;使用 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');
}
}

Next, I created a class "paint" using the model.接下来,我使用模型创建了一个“paint”类。 But note that the recent version of laravel don't have the model folder specified explicitly.但请注意,最新版本的 laravel 没有明确指定模型文件夹。 So when I ran the code below on the command prompt I was able to create the paint class所以当我在命令提示符下运行下面的代码时,我能够创建油漆类

php artisan make:model paint

Lastly, I tried updating the created table, paintings, via routes.php using the code below..最后,我尝试使用下面的代码通过 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";

  });

please I am new to laravel, so I will appreciate any help to get this resolved.请我是laravel的新手,所以我会很感激任何帮助解决这个问题。 I am presently stranded.我目前被困。 Lest I forget, The error message is shown below免得我忘记了,错误信息如下所示


Whoops, looks like something went wrong.哎呀,看起来出事了。

1/1 FatalErrorException in routes.php line 18: Class 'Paint' not found route.php 第 18 行中的 1/1 FatalErrorException:找不到“Paint”类

in routes.php line 18在 routes.php 第 18 行

Ah, this is a simple error.啊,这是一个简单的错误。 Can you copy-paste the whole content of the file in if that is not all of it.如果不是全部,您能否将文件的全部内容复制粘贴进去。

Also the error for this is because you haven't brought in the class from the autoloader.同样的错误是因为您没有从自动加载器中引入类。 In Laravel, everything is namespaced.在 Laravel 中,一切都是命名空间的。 In your Paint Model, at the top you will see namespace App , if you moved the Paint model into say a models directory, you need to namespace the class App\\Models;在你的画图模型中,在顶部你会看到namespace App ,如果你将画图模型移动到一个模型目录中,你需要命名空间类App\\Models;

To fix this error, at the top of the routes.php file, write use App\\Paint;为了修复这个错误,在routes.php文件的顶部,写use App\\Paint; . . But you should really send that route to a controller to separate the code from the routes.但是您真的应该将该路由发送到控制器以将代码与路由分开。

Let me know if that helps.如果这有帮助,请告诉我。

it's normal the class Pain not found, check what you done here :找不到类 Pain 是正常的,请检查您在此处完成的操作:

php artisan make:model paint

and on your routes file :并在您的路线文件中:

$paintings = new Paint;

try to correct your model class name to this Paint , and when you want to instanciate it don't forget the namespace by default for the generated model class , so you routes file will look like this尝试将您的模型类名称更正为此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