//From the Round.php model
function group () {
return $this->belongsTo('App\Group');
}
//From the RoundController.php
//Get all the rounds (this will return an array with objects)
$rounds = Round::all();
//Loop thru the rounds
foreach ($rounds AS $round) {
//get the group relation, and show the group name
echo $round->group->name;
}
/*
* Mind you a relation with hasOne/hasMany/belongsTo/belongsToMany are ATTRIBUTES of the model
* it is not a function, so do not use () at the end (it will not show a thing)
*/
//From the Round.php model
public function peers() {
return $this->hasMany('App\Peer');
}
//From the RoundController.php
//Get all the rounds (this will return an array with objects)
$rounds = Round::all();
//Loop thru the rounds
foreach ($rounds AS $round) {
//get the group relation, and show the group name
echo $round->peers->id
}
^^^ This will not work...
//From the Round.php model
public function peers() {
return $this->hasMany('App\Peer');
}
//From the RoundController.php
//Get all the rounds (this will return an array with objects)
$rounds = Round::all();
//Loop thru the rounds
foreach ($rounds AS $round) {
//get the group relation, and show the group name
foreach ($round->peers AS $peer) {
echo $peer->id
}
}
//From the Peer model
public function reviewer() {
return $this->belongsTo('App\User', 'reviewer_id');
} ^^^^^^^^^^^^^ Alternative
//get all the peers
$peers = Peer::all();
//Loop thru the rounds
foreach ($peers AS $peer) {
$peer->reviewer->name; // Gets the user name from the reviewer
}
//From the Group.php model
public function parent() {
return $this->belongsTo('App\Group', 'group_id');
}
public function users() {
return $this->belongsToMany('App\User', 'user_groups', 'group_id', 'user_id');
}
public function students() {
return $this->users()->whereHas('roles', function ($q) {
$q->Where('ident', '=', 'student');
})->get();
}
//From the PeerController.php
public function create(Group $group) {
if ($group->parent) {
$parent = Group::where('id', $group->group_id)->first();
$users = $parent->teachers();
}
if (!$group->parent) {
$users = $group->teachers();
}
$rubrics = Rubric::all();
return view('peer.create', compact('rubrics', 'users', 'group'));
}