Here is my tables: https://ibb.co/SNtNfjW
and function to return surveys to users who are in admin group and show surveys which are created by current logged in user.
public static function getSurveys(Request $request)
{
if(Role::find(1)->users()->where('id', auth()->id())->exists()) //if user is in first group (admin group) show all surveys
{
$surveys = Survey::orderBy('id', 'ASC')->get();
}
else //else just show auth users surveys
{
$surveys = Survey::where('user_id', auth()->id())->id())->orderBy('id', 'ASC')->get();
}
How could I display surveys for users that are not in admin group but in any other group? Every survey has a field "subject" and "subject" values has the same names as values in table "roles" field "name".
This is what i figured out, but i can't write code like this because there are many groups and survey table column "subject" have many different values.
else if(Role::find(3)->users()->where('id', auth()->id())->exists()) //if user is in the third group
{
$surveys = Survey::where('user_id', auth()->id())->orWhere('subject', Role::where('id', 3)->value('name'))->orderBy('id', 'ASC')->get();
}
I am stuck for days and hoping for some help.