播种器命令不起作用Laravel 5.4

时间:2017-08-01 12:00:50

标签: php laravel symfony

我是laravel的新手我正在尝试按照我为DB创建数据库播种器的教程。所以为此,我使用了php artisan make:seeder ArticlesTableSeeder命令

播种机

class ArticlesTableSeeder extends Seeder
{
    public function run()
    {
        // Let's truncate our existing records to start from scratch.
        Article::truncate();

        $faker = \Faker\Factory::create();

        // And now, let's create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            Article::create([
                'title' => $faker->sentence,
                'body' => $faker->paragraph,
            ]);
        }
    }
}

现在,当我运行php artisan db:seed --class=ArticlesTableSeeder命令时,我收到了此错误

[Symfony\Component\Debug\Exception\FatalThrowableError] Class 'Article' not found

我有一篇文章模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
   protected $fillable = ['title', 'body'];
}

我在这里做错了什么?我搜索了这个错误,发现它们的最大值是由于拼写错误造成的。我想我在这里没有拼写错误。这段代码有什么问题?

帮助将不胜感激。

4 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/white"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <include layout="@layout/toolbar" android:id="@+id/drawer_toolbar"/>
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/framelayout"/>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main">
       <!-- app:menu="@menu/activity_main_drawer"-->

        <ExpandableListView
            android:id="@+id/navigationmenu"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start|right"
            android:divider="#f0f0f0"
            android:dividerHeight="1.5dp"
            android:indicatorEnd="?android:attr/expandableListPreferredItemIndicatorRight"
            android:layout_marginTop="170dp"
            android:background="@android:color/white"
            android:visibility="visible">
        </ExpandableListView>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:1)

您错过了将模型包含在控制器中,因此您获得了class not found error。在课程

之前将此行添加到您的控制器
use App\Article;

答案 2 :(得分:1)

您错过了将模型包含在控制器中的问题,因此出现类未找到错误。在课程开始之前将此行添加到您的控制器中

使用App \ Models \ Article;

答案 3 :(得分:-1)

您应该做的只是在\App\之前添加Article或者只是添加use \App\Article

class ArticlesTableSeeder extends Seeder
{
    public function run()
    {
        // Let's truncate our existing records to start from scratch.
        \App\Article::truncate();

        $faker = \Faker\Factory::create();

        // And now, let's create a few articles in our database:
        for ($i = 0; $i < 50; $i++) {
            \App\Article::create([
                'title' => $faker->sentence,
                'body' => $faker->paragraph,
            ]);
        }
    }
}