从数据透视表中选择所有记录

时间:2015-03-14 12:10:46

标签: laravel eloquent laravel-5

我有很多关系,从PageUser

class User extends Model {
    ...
    public function visits() {
        return $this->belongsToMany('App\Page')->withTimestamps();
    }
    ...
}

class Page extends Model {
    ...
    public function visitedBy() {
        return $this->belongsToMany('App\User')->withTimestamps();
    }
    ...
}

使用以下数据透视表:

    Schema::create('page_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('page_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->timestamps();

        $table->foreign('page_id')
              ->references('id')
              ->on('pages')
              ->onDelete('cascade')
              ->onUpdate('cascade');

        $table->foreign('user_id')
              ->references('id')
              ->on('users')
              ->onDelete('cascade')
              ->onUpdate('cascade');
    });

如何选择K最近的网页访问次数,其中K是已知的正整数?

0 个答案:

没有答案