0

我刚开始学习 PHP 和 Laravel 7,没有任何经验。我认为 Artisan 和我遇到了一些信任问题,我不知道为什么。:) 我达到了播种步骤,但我遇到了一个错误,我需要您的专业知识以简单的方式解决。以下是我在遇到此问题之前执行的先前步骤。

我创建产品控制器和模型

php artisan make:model Product -m -c -resource

数据库

class CreateProductsTable extends Migration
{
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name', 100);
            $table->string('image')->nullable();
            $table->string('type');
            $table->decimal('price', 2);
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
}

模型

class Product extends Model
{
    protected $table = 'product';
    protected $fillable = ['name', 'image', 'type', 'price', 'description'];
}

控制器

class ProductController extends Controller
{

    public function __construct()
    {
       $this->middleware('auth');
    }

    public function index()
    {
        $product = DB::table('products')->get();
    }
}

应用服务提供者

class AppServiceProvider extends ServiceProvider
{
    public function register()
    {
        Schema::defaultStringLength(191);
    }
}

之后,我创建了ProductTableSeeder

php artisan make:seeder ProductTableSeeder

class ProductTableSeeder extends Seeder
{
    public function run()
    {
        Product::insert([
            'name' => 'iphone',
            'type' => 'smart phone',
            'price' => 1000.98,
            'description' => 'Cillum sint dolore sint labori',
        ]);

        Product::insert([
            'name' => 'Galaxy',
            'type' => 'tablets',
            'price' => 2000.50,
            'description' => 'Cillum sint dolore sint labori',
        ]);

        Product::insert([
            'name' => 'Sony',
            'type' => 'TV',
            'price' => 3000,
            'description' => 'Cillum sint dolore sint labori',
        ]);
    }
}

然后,叫播种机

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        // $this->call(UserSeeder::class);
        $this->call(ProductTableSeeder::class);
    }
}

然后,composer dump-autoload

最后,输入播种命令后

php artisan migrate:fresh --seed

我不断收到以下错误。

Dropped all tables successfully.
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.51 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.12 seconds)
Migrating: 2020_04_13_063011_create_products_table
Migrated:  2020_04_13_063011_create_products_table (0.18 seconds)
Seeding: ProductTableSeeder

   Illuminate\Database\QueryException

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'e-commerce.product' doesn't exist (SQL: insert into `product`

( name, type, price, description) 值(iphone、智能手机、1000.98、Cillum sint dolore sint labori))

  at C:\wamp64\www\E-Commerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671
    667|         // If an exception occurs when attempting to run a query, we'll format the error
    668|         // message to include the bindings with SQL, which will make this exception a
    669|         // lot more helpful to the developer instead of just the database's errors.
    670|         catch (Exception $e) {
  > 671|             throw new QueryException(
    672|                 $query, $this->prepareBindings($bindings), $e
    673|             );
    674|         }
    675|

  • A table was not found: You might have forgotten to run your migrations. You can run your migrations using `php artisan migrate`.
    https://laravel.com/docs/master/migrations#running-migrations

  1   C:\wamp64\www\E-Commerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
      PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'e-commerce.product' doesn't exist")

  2   C:\wamp64\www\E-Commerce\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
      PDO::prepare("insert into `product` (`name`, `type`, `price`, `description`) values (?, ?, ?, ?)")
4

1 回答 1

-2

这个来自@Ersoy 的回答

@kha 你要么删除$table

属性或修改它

protected $table = 'products';

帮我解决了这个问题,这是解决方案。

在模型中,我在表名的末尾添加了“s”(忘记将其设为复数)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $table = 'products'; //(forgot to make it plural)

    protected $fillable = ['name','image','type','price','description',
                          ];
}

控制器:

<?php

namespace App\Http\Controllers;

use App\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
       //$this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
         $prod = Product::paginate(20);
         return view('allproducts');

    // $products = [0=>['name'=>'iphone','category'=>'smart phone', 'price'=>1000,],
    //              1=>['name'=>'Galaxy','category'=>'tablets', 'price'=>2000],
    //              2=>['name'=>'Sony','category'=>'TV', 'price'=>3000]];
    // return $products;

     //return view('allproducts',compact('product'));
}

将价格变量从十进制更改为双倍

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->id();

        $table->string('name', 100);
        $table->string('image')->nullable();
        $table->string('type');
        $table->double('price', 2);//change the variable
        $table->text('description')->nullable();

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

然后运行命令

php artisan migrate:refresh --seed

Rolling back: 2020_04_13_063011_create_products_table
Rolled back:  2020_04_13_063011_create_products_table (0.08 seconds)
Rolling back: 2019_08_19_000000_create_failed_jobs_table
Rolled back:  2019_08_19_000000_create_failed_jobs_table (0.06 seconds)
Rolling back: 2014_10_12_100000_create_password_resets_table
Rolled back:  2014_10_12_100000_create_password_resets_table (0.1 seconds)
Rolling back: 2014_10_12_000000_create_users_table
Rolled back:  2014_10_12_000000_create_users_table (0.08 seconds)
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (0.39 seconds)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (0.52 seconds)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (0.17 seconds)
Migrating: 2020_04_13_063011_create_products_table
Migrated:  2020_04_13_063011_create_products_table (0.12 seconds)
Seeding: ProductTableSeeder
Seeded:  ProductTableSeeder (0.02 seconds)
Database seeding completed successfully.

我想工匠从一开始就不信任我是对的。:)

于 2020-05-13T21:48:14.707 回答