I have situation in laravel 5.1 with eloquent. That is very hard for me at this time to get the solution.
I have 7 tables in database which are not related to each other, but I want to get results with joining them all and with where clause.
My Tables relations are:
lifegroups -> Campuses -> locations -> address
and
lifegroups -> grouptype -> groupuser -> user // this is confusing little bit
and
lifegroups -> schedule -> recurrance
Notice the lifegroup table is associated with multiple other tables.
My Existing Query is:
$campusGroups = DB::table('campuses')
->Join('lifegroups','campuses.f1_id', '=', 'lifegroups.campusId')
->Join('locations', 'locations.group_id', '=', 'lifegroups.f1Id')
->Join('addresses', 'addresses.location_id', '=', 'locations.id')
->Join('groups_users', 'groups_users.groupId', '=', 'lifegroups.id')
->Join('users', 'groups_users.userId', '=', 'users.id')
->distinct() //'users.first_name', 'users.last_name',
->select('lifegroups.*', 'campuses.name AS campusname', 'addresses.address', 'addresses.address2', 'addresses.city', 'addresses.province');
$groupLeaders = DB::table('campuses')
->Join('lifegroups', 'campuses.f1_id', '=', 'lifegroups.campusId')
->Join('groups_users', 'groups_users.groupId', '=', 'lifegroups.id')
->Join('users', 'groups_users.userId', '=', 'users.id')
->distinct()
->select('lifegroups.id', 'users.first_name', 'users.last_name');
$groupRecurrences = DB::table('campuses')
->Join('lifegroups', 'campuses.f1_id', '=', 'lifegroups.campusId')
->Join('schedules', 'schedules.event_id', '=', 'lifegroups.eventid')
->Join('recurrences', 'recurrences.schedule_id', '=', 'schedules.id')
->distinct()
->select('recurrences.occurOnFriday', 'recurrences.occurOnSaturday', 'recurrences.occurOnMonday', 'recurrences.occurOnSunday', 'recurrences.occurOnThursday', 'recurrences.occurOnTuesday', 'recurrences.occurOnWednesday', 'recurrences.recurrenceFrequency', 'recurrences.recurrenceFrequencyMonthly', 'recurrences.recurrenceFrequencyWeekly', 'schedules.start_date', 'schedules.recurrence', 'lifegroups.id AS groupId');
if (Session::has('campus')){
$campusId = Session::get('campus');
$searchObj = $campusGroups->where('campuses.id', intval($campusId));
$groupLeaders = $groupLeaders->where('campuses.id', intval($campusId));
$groupRecurrences = $groupRecurrences->where('campuses.id', intval($campusId));
$searchArray['campusId'] = $campusId;
}
if (Session::has('gender')){
$gender = Session::get('gender');
$searchObj = $campusGroups->where('lifegroups.gender', $gender);
$groupLeaders = $groupLeaders->where('lifegroups.gender', $gender);
$groupRecurrences = $groupRecurrences->where('lifegroups.gender', $gender);
$searchArray['gender'] = $gender;
} else {
$searchArray['gender'] = 'N/A';
}
if (Session::has('marital_status')){
$marital_status = Session::get('marital_status');
$searchObj = $campusGroups->where('lifegroups.marital_status', $marital_status);
$groupLeaders = $groupLeaders->where('lifegroups.marital_status', $marital_status);
$groupRecurrences = $groupRecurrences->where('lifegroups.marital_status', $marital_status);
$searchArray['marital_status'] = $marital_status;
} else {
$searchArray['marital_status'] = 'N/A';
}
if (Session::has('age')){
$age = Session::get('age');
$searchObj = $campusGroups->where('lifegroups.startAgeRange', $age);
$groupLeaders = $groupLeaders->where('lifegroups.startAgeRange', $age);
$groupRecurrences = $groupRecurrences->where('lifegroups.startAgeRange', $age);
$searchArray['age'] = $age;
} else {
$searchArray['age'] = 'N/A';
}
if (Session::has('keyword')){
$keyword = Session::get('keyword');
$searchObj = $campusGroups->where('lifegroups.name', 'Like', '%'. $keyword . '%');
//->orWhere('lifegroups.description', 'Like', '%'.$keyword.'%')
//->orWhere('users.first_name', 'Like', '%'.$keyword.'%');
$groupLeaders = $groupLeaders->where('lifegroups.name', 'Like', '%'. $keyword . '%');
$groupRecurrences = $groupRecurrences->where('lifegroups.name', 'Like', '%'. $keyword . '%');
$searchArray['keyword'] = $keyword;
} else {
$searchArray['keyword'] = 'N/A';
}
$campusGroupsTemp = $campusGroups->get();
$campusGroups = [];
$recurrenceGroups = [];
$groupLeaders = $groupLeaders->get();
$groupRecurrences = $groupRecurrences->get();
So this is a very long process and I can't add further search filters. Do you guys have any better idea how to do it with Laravel Eloquent, like
Lifegroups::with(['campuses', 'locations', 'addresses', 'grouptype', 'groupuser', 'users', 'schedules', 'recurrences'])
->where(['lifegroups.name', '=' 'name_value'],['lifegroups.description','like', '%like_valie%'], ['user.first_name', 'like', 'first_name_value'])
->get()->toArray();
If you are confused like me then only let me know how to do below mentioned thing with above type of tables scenario.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire