我动态地使用MySQL数据库成功地将一列添加到上一个表中

时间:2017-07-01 14:01:31

标签: laravel

我动态地使用MySQL数据库成功地向上一个表添加了一个列,但是当我填写表单运行并将其发送到本地服务器中的数据库时,它显示:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'forge. posts' doesn't exist (SQL: select count(*) as aggregate from ` posts` where ` slug ` = hihello)

我的迁移代码表'帖子'

<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

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

用于插入名为&#39; slug&#39;的新列。在&#39;帖子&#39;表

 <?php

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

class AddSlugToUsers extends Migration
{



    public function up(){

      Schema::table('posts',function(Blueprint $table){
        $table->string('slug')->unique()->after('title');

      });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down(){

     Schema::table('posts',function(Blueprint $table){
     $table->dropColumn('slug');
        });


    }
   }

成功插入&#39; slug&#39;列,但问题是将表单发布到数据库。

<?php  


namespace App\Http\Controllers;
            use Illuminate\Http\Request;
             use App\Http\Requests;
           use App\Http\Controllers\Controller;
          use App\Post;
         use Session;

          class PostController extends Controller
          {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
           public function index()
            {
            $posts = Post::orderBy('id', 'desc')->paginate(10);
            return view('posts.index')->withPosts($posts);
            }

        /**
         * Show the form for creating a new resource.
         *
         * @return \Illuminate\Http\Response
         */
             public function create()
             {
            return view('posts.create');
             }

        /**
         * Store a newly created resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Illuminate\Http\Response
         */
             public function store(Request $request)
            {
            // validate the data
            $this->validate($request, array(
                    'title' => 'required|max:255',
                    'slug' 
                   =>'required|min:5|alpha_dash|min:5|max:255|unique: 
             posts, slug ',
                    'body'  => 'required'));

            // store in the database
            $post = new Post;

            $post->title = $request->title;
            $post->slug =  $request->title;
            $post->body = $request->body;

            $post->save();

            Session::flash('success', 'The blog post was successfully 
            save!');

            return redirect()->route('posts.show', $post->id);
             }

        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
            public function show($id)
             {
            $post = Post::find($id);
            return view('posts.show')->withPost($post);
             }

        /**
         * Show the form for editing the specified resource.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
            public function edit($id)
            {
            // find the post in the database and save as a var
            $post = Post::find($id);
            // return the view and pass in the var we previously created
            return view('posts.edit')->withPost($post);
            }

        /**
         * Update the specified resource in storage.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
            public function update(Request $request, $id)
             {
            // Validate the data
            $this->validate($request, array(
                    'title' => 'required|max:255',

             'slug'=>'required|alpha_dash|min:5|max:255|unique:posts,slug',
                    'body'  => 'required'
                ));
            // Save the data to the database
            $post = Post::find($id);

            $post->title = $request->input('title');
            $post->title=$request->input('slug');
            $post->body = $request->input('body');

            $post->save();

            // set flash data with success message
            Session::flash('success', 'This post was successfully saved.');

            // redirect with flash data to posts.show
            return redirect()->route('posts.show', $post->id);
           }

         /**
         * Remove the specified resource from storage.
         *
         * @param  int  $id
         * @return \Illuminate\Http\Response
         */
            public function destroy($id)
            {
            $post = Post::find($id);

            $post->delete();
            //for sesssion
            Session::flash('success', 'The post was successfully deleted.');
            return redirect()->route('posts.index');
               }
            }

1 个答案:

答案 0 :(得分:0)

&#39;伪造。帖子&#39;这看起来像表名帖子之前有一个空格。如果有s

,你检查了你的模型吗?