I have the following tables in a db which are as follows (not all of the columns but minimised for clarity):
admins (id, community_id, email, password, level)
community (community_id, community_name)
community_results (community_result_id, community_session_id)
community_sessions (community_session_id, community_id)
I have setup the associated models and within my admin controller I have the following code that simply grabs all the results in the database into a laravel collection (and this works fine). The admin table contains two types of admin user - one is the 'superuser' that has access to every single row in the db, and the other type is a 'community admin' and will only have access to their community results (defined by the level column in the admins table).
When logged into the admin as a 'community admin' I want to only get results for their community (admins.community_id is the foreign key in this instance that relates this admin to a community).
e.g John Doe is a 'community admin' for the 'ACME Community' and belongs to the community_id 5, when logged in he will only get 'community results' for all of the 'community sessions' that relate to that particular community (community_id is the foreign key in the community_sessions table).
Can anyone suggest the best way to do this? Thanks in advance
class AdminResultController extends Controller
{
public function index()
{
// if logged in as the 'superuser' admin get ALL results
if (Auth::guard('admin')->user()->level == 1)
{
$results = CommunityResult::all();
} else {
// logged in as 'standard' admin get only their community results
$results = new CommunityResult;
// how do I get collection of results for this community only
}
}
}
// CommunityResult.php (model)
class CommunityResult extends Model
{
public $primaryKey = 'community_result_id';
public function session()
{
return $this->hasOne('App\CommunitySession', 'community_session_id', 'community_session_id');
}
}
// CommunitySession.php (model)
class CommunitySession extends Model
{
public $primaryKey = 'community_session_id';
public function community()
{
return $this->belongsTo('App\Community', 'community_id');
}
public function results()
{
return $this->hasMany('App\CommunityResult', 'community_session_id');
}
}
// Community.php (model)
class Community extends Model
{
public $table = 'community';
public $primaryKey = 'community_id';
public function sessions()
{
return $this->hasMany('App\CommunitySession', 'community_id');
}
}
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/28ZQRVG
via IFTTT
Aucun commentaire:
Enregistrer un commentaire