提问者:小点点

SQLSTATE[42S02]:找不到基表或视图: 1146表'blog.posts'不存在


这是拉威尔的博客

我正在建立数据库并创建表,当我运行程序时,程序有警告:

配置/数据库。php

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ];

2019年4月1日135051日创建职位表

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('title');
        $table->text('content');
    });
}

2019年04月01日134543年创建表格

 public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}

2019\u 04\u 01\u 134543\u创建标签\u表格

  public function up()
{
    Schema::create('tags', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('name');
    });
}

这一行在Post类中:

 protected $fillable = ['title', 'content'];

这一行是在postControoler.php

  public function postAdminCreate(Store $session, Request $request)
{
    $this->validate($request, [
        'title' => 'required|min:5',
        'content' => 'required|min:10'
    ]);
    $post = new Post([
        'title' => $request->input('title'),
        'content' => $request->input('content')
    ]);
    $post->save();

    return redirect()->route('admin.index')->with('info', 'Post created, Title is: ' . $request->input('title'));
}

2014_10_12_000000_create_users_table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('title');
        $table->string('content');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

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

这是博客文件夹中的post.blade.php

@extends('layouts.master')

@section('content')
<div class="row">
    <div class="col-md-12">
        <p class="quote">{{ $post['title']  }}</p>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <p>{{ $post['content'] }}</p>
    </div>
</div>
@endsection

首先,我运行

php artisan migrate

部长说:

 Migrating: 2014_10_12_000000_create_users_table

    Illuminate\Database\QueryException  : SQLSTATE[42S01]: Base table or view 
    already exists: 1050 Table 'users' already exists (SQL: create table `users` 
    (`id` bigint unsigned not null auto_increment primary key, `title` 
    varchar(255) not null, `content` varchar(255) not null, `email` varchar(255) 
    not null, `email_verified_at` timestamp null, `password` varchar(255) not 
    null, `remember_token` varchar(100) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at C:\xampp\htdocs\getting-started\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")
      C:\xampp\htdocs\getting-started\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

  2   PDOStatement::execute()
      C:\xampp\htdocs\getting-started\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458

当我创建帖子并单击“提交”时,请说“警告”

我怎样才能修好它?


共3个答案

匿名用户

使用php artisan迁移:新鲜命令迁移数据库中的表

匿名用户

服务提供者。php

public function boot()
{
    Schema::defaultStringLength(191);
    if (Schema::hasTable('blogs')) {
        $blog = Blog::first();
        View::share('blog',$blog);
    }
}

匿名用户

您可以执行以下步骤:

  1. 数据库中删除所有表

现在试试。