Sto cercando di ottenere le hackathon più popolari che richiedono l'ordine dal rispettivo hackathon partipants->count()
. Scusa se è un po 'difficile da capire.Ordine Laravel Conteggio relazioni
Ho un database con il seguente formato:
hackathons
id
name
...
hackathon_user
hackathon_id
user_id
users
id
name
Il modello Hackathon
è:
class Hackathon extends \Eloquent {
protected $fillable = ['name', 'begins', 'ends', 'description'];
protected $table = 'hackathons';
public function owner()
{
return $this->belongsToMany('User', 'hackathon_owner');
}
public function participants()
{
return $this->belongsToMany('User');
}
public function type()
{
return $this->belongsToMany('Type');
}
}
E HackathonParticipant
è definito come:
class HackathonParticipant extends \Eloquent {
protected $fillable = ['hackathon_id', 'user_id'];
protected $table = 'hackathon_user';
public function user()
{
return $this->belongsTo('User', 'user_id');
}
public function hackathon()
{
return $this->belongsTo('Hackathon', 'hackathon_id');
}
}
ho provato Hackathon::orderBy(HackathonParticipant::find($this->id)->count(), 'DESC')->take(5)->get());
ma mi sento come se avessi fatto un grosso errore (forse il $ this-> id), perché non funziona affatto.
Come dovrei provare a ottenere le hackathon più popolari che si basano sul numero più alto di partecipanti hackathonParticipants?
Come cambiare asending/desecnding? – FooBar
'sortBy (Chiusura $ callback, $ options = SORT_REGULAR, bool $ descending = false)' Basta impostare il 3 ° parametro su true. – user3158900
ordina per non farlo a livello di database ... quindi non funzionerà correttamente con il paging! Buono per quando si restituisce un set completo anche se .. –