Connection.php第651行中的Laravel QueryException:SQLSTATE [42S02]:找不到基表或视图

时间:2015-10-21 08:42:45

标签: php mysql laravel laravel-5

我是Laravel的新手并且有错误。 当我尝试检查我的页面时,我收到此错误:

QueryException in Connection.php line 651: SQLSTATE[42S02]: Base table or view not found: 1146 Table "scotchbox.likes" doesn't exist (SQL: select "users".*, "users"."name", "projects"."title" from "likes" inner join "users" on "user_id" = "users"."id" inner join "projects" on "project_id" = "projects"."id")

这是我的迁移:

<?php

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

class CreateLikesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('likes', function(Blueprint $table){
            $table->increments('id');
            $table->integer('project_id');
            $table->integer('user_id');
            $table->softDeletes();
            $table->timestamps();
        });
    }

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

我的功能:

public function activity()
{
    $likes = DB::table('likes')
        ->join('users', 'user_id', '=', 'users.id')
        ->join('projects', 'project_id', '=', 'projects.id')
        ->select('users.*', 'users.name', 'projects.title')
        ->get();

    return view('user.activity', ['likes' => $likes]);
}

我已经回滚了我的迁移,刷新了它们......但它并没有帮助......

任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分:3)

您应该在控制台中运行迁移。它在db中创建likes表。

php artisan migrate

如果您想了解迁移:http://laravel.com/docs/5.1/migrations

答案 1 :(得分:2)

试试这个:

使用特定的表名创建模型,在文本编辑器中打开模型添加以下行:

protected $table = 'tablename';

所以它看起来像这样:

class Gallery extends Model
{
    protected $table = 'tablename';
}

答案 2 :(得分:0)

运行迁移:

php artisan migrate