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

时间:2019-04-08 15:32:43

标签: laravel warnings laravel-5.8

[标题] [1] ## Laravel 5.8

这是Laravel的博客

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

SQLSTATE [42S02]:找不到基本表或视图:1146表'blog.posts'不存在(SQL:插入到`posts`中(`title`,`content`,`updated_at`,`created_at` )值(asdasdasdas,此博客文章将使您正确掌握laravel,2019-04-08 14:54:16,2019-04-08 14:54:16))

config / database.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_04_01_135051_create_posts_table

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

2019_04_01_134543_create_likes_table

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

2019_04_01_134543_create_tags_table

  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

当我创建帖子并单击提交时说警告

我该如何解决?

SQLSTATE [42S02]:找不到基本表或视图:1146表'blog.posts'不存在(SQL:插入到`posts`中(`title`,`content`,`updated_at`,`created_at` )值(asdasdasdas,此博客文章将使您正确掌握laravel,2019-04-08 14:54:16,2019-04-08 14:54:16)) 黄色“> SQLSTATE [42S02]:找不到基本表或视图:1146表'blog.posts'不存在(SQL:插入到`posts`中(`title`,`content`,`updated_at, ``created_at`)值(asdasdasdas,此博客文章将使您正确掌握laravel,2019-04-08 14:54:16,2019-04-08 14:54:16))

3 个答案:

答案 0 :(得分:0)

您可以执行以下步骤:

  1. database中删除所有表格。
  2. 再次运行php artisan migrate
  3. 检查posts表是否在database中创建

现在尝试。

答案 1 :(得分:0)

使用php artisan migrate:fresh命令在数据库中全新迁移表

答案 2 :(得分:0)

ServiceProvider.php

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