表没有找到一对多的关系laravel

时间:2018-06-11 17:12:13

标签: laravel laravel-5

我的数据库

Schema::create('request_topics', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('status')->nullable()->default('Pending');
        $table->integer('request_category_id')->unsigned()->index();
        $table->foreign('request_category_id')->references('id')->on('request_categories')
                    ->onDelete('cascade')
                    ->onUpdate('cascade');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')
                    ->onDelete('cascade')
                    ->onUpdate('cascade');
        $table->timestamps();
    });

Schema::create('request_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title')->unique();
        $table->string('slug')->unique();
        $table->string('description');
        $table->boolean('pin')->default(false);
        $table->timestamps();
    });

当我像{{ count($requestcategory->requesttopics) }}一样工作时它的工作正常,但是当我拿到

$requesttopics = RequestTopic::whereHas('requestcategories', function ($query) use($slug){
            $query->where('slug', $slug);
        })->paginate('20');

找不到错误列

  

未找到列:1054未知列' request_topics.requestcategories_id'在' where子句' (SQL:从request_topics

中选择count(*)作为聚合

我的代码出了问题,或者我做错了什么 帮助我!

1 个答案:

答案 0 :(得分:1)

您确定模型中有requestcategories关系吗?如果是,则应检查第二和第三个参数(本地和外键)

public function requestcategories(){

  return $this->belongsTo('App\RequestCategory', 'request_category_id', 'id');

}