0
votes

[Heading][1] ##Laravel 5.8

This is the weblog with Laravel

I'm setting up a database and create the table when i run the program, the program has warning:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'blog.posts' doesn't exist (SQL: insert into `posts` (`title`, `content`, `updated_at`, `created_at`) values (asdasdasdas, This blog post will get you right on track with 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');
    });
}

this line is in the Post class :

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

this line is in 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');
}
}

this is the post.blade.php in blog folder

@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

First I run the

php artisan migrate

the cmd say :

 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

When i create post and click submit say warning

How can i fix it?

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'blog.posts' doesn't exist (SQL: insert into `posts` (`title`, `content`, `updated_at`, `created_at`) values (asdasdasdas, This blog post will get you right on track with laravel, 2019-04-08 14:54:16, 2019-04-08 14:54:16)) yellow">SQLSTATE[42S02]: Base table or view not found: 1146 Table 'blog.posts' doesn't exist (SQL: insert into `posts` (`title`, `content`, `updated_at`, `created_at`) values (asdasdasdas, This blog post will get you right on track with laravel, 2019-04-08 14:54:16, 2019-04-08 14:54:16))
3
Not a solution but try adding this in your post model protected $table = 'posts'; - Vipertecpro
Did you run php artisan migrate? - mdexp
yes I run the php artisna migrate - hamidprog2003
try php artisan migrate:fresh - Tharaka Dilshan

3 Answers

1
votes

usephp artisan migrate:fresh command to migrate table in database freshly

0
votes

ServiceProvider.php

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

You can do those steps :

  1. Remove all the table from your database.
  2. run php artisan migrate again.
  3. Check posts table is created in database

And now try.