/** * Run the migrations. * * @return void */ public function up() { $pivotTable = Follow::pivotTable(); $usersTable = Follow::usersTable(); Schema::create($pivotTable, function (Blueprint $table) use($usersTable) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on($usersTable)->onUpdate('cascade')->onDelete('cascade'); $table->integer('follow_id')->unsigned(); $table->foreign('follow_id')->references('id')->on($usersTable)->onUpdate('cascade')->onDelete('cascade'); $table->timestamps(); }); }
/** * @return mixed */ public static function mostFollowed() { return User::leftJoin(Follow::pivotTable() . ' AS uf', 'uf.follow_id', '=', Follow::usersTable() . '.id')->groupBy('uf.follow_id')->having(DB::raw('COUNT(uf.follow_id)'), '>', 0)->orderBy(DB::raw('COUNT(uf.follow_id)'), 'desc'); }