我们如何通过laravel eloquent加入条件结果?

时间:2015-10-23 18:53:24

标签: php laravel laravel-5 eloquent laravel-5.1

我在laravel 5.1中的情况很有说服力。我现在很难找到解决方案。

我在数据库中有7个表彼此无关,但是我希望得到结果加入all和where子句。

我的表关系是:

lifegroups -> Campuses -> locations -> address

lifegroups ->  grouptype -> groupuser -> user  // this is confusing little bit

lifegroups -> schedule -> recurrance

请注意,lifegroup表与多个其他表相关联。

我的现有查询是:

$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();

所以这是一个非常漫长的过程,我无法添加更多搜索过滤器。你们有更好的想法如何使用Laravel Eloquent,比如

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();

如果你像我一样困惑,那么只让我知道如何使用上述类型的表格方案做下面提到的事情。

1 个答案:

答案 0 :(得分:0)

使用Eloquent可以很容易地假设您正确设置了模型。只需使用点符号来处理嵌套。

$data = Lifegroups::with([
    'campuses.locations.address',
    ['grouptype.groupuser.user', function($q) use ($first_name) {
        // Handle the constraints on the users table here.
        $q->where('name', 'like', $first_name)
    }],
    'schedule.recurrance'
])
    // Handle any constraints on the Lifegroups table here
    ->where('name', '=', 'name_value')
    ->where('description','like', '%like_valie%')
    ->get();

如果你需要为不同的表添加更多约束,只需将另一个参数添加到我们传递到with函数的数组中,直到你需要使用nexting并在其中传入一个函数将像我做的用户表一样设置约束。