是否可以基于if条件使用雄辩的关系?

时间:2019-08-08 08:01:27

标签: php laravel eloquent

是否可以基于if条件使用雄辩的关联关系选项。 我正在尝试基于if条件创建对象。请查看打击代码。每次最后一个条件对象返回时。但我需要完整的对象。如果看到代码,则有两个对象CountryDetails和stateDetails。但是此代码仅返回最后一个对象stateDetails。我需要CountryDetails和stateDetails。

如果您看到代码,则有两个对象CountryDetails和stateDetails。但是此代码仅返回最后一个对象stateDetails。我需要对象CountryDetails和stateDetails。

  if($registrationFieldObj->country == true){
                $userDetails->with(['userMeta'=>function($query){
                    $query->with(['CountryDetails'=>function($query2){
                        $query2->select('id','country_name');
                    }]);
                }]);
            }

            if($registrationFieldObj->state == true){
                $userDetails->with(['userMeta'=>function($query){
                    $query->with(['stateDetails'=>function($query2){
                        $query2->select('id','country_id','state_name');
                    }]);
                }]);
            }
{   
     "has_error": 0,
        "msg": "Successfully Logged in",
        "api_token": "ey8S7XN3vdNgFravuw6Zmt8H3n2ol2UX1GA9q4l7XdRcitQymZ7ETv2W4lAk",
        "user": {
            "id": 2,
            "name": "Jhon Smith",
            "email": "jhon@yopmail.com",
            "phone": "9123378019",
            "usertype": "APP",
            "profile_pic": "no_profile_img.png",
            "status": "A",
            "created_at": "2019-07-31 05:17:34",
            "updated_at": "2019-08-08 07:36:30",
            "user_meta": {
                "id": 1,
                "user_id": 2,
                "address": "Kolkata",
                "country_id": 101,
                "state_id": 39,
                "city_id": 5226,
                "pincode": "700001",
                "device_id": 458945132565,
                "device_token": "fsas576dfsbsfjn6qe7q",
                "device_type": "A",
                "updated_at": "2019-08-07 13:03:10",
                "created_at": null,
                "state_details": {
                    "id": 39,
                    "country_id": 101,
                    "state_name": "Uttarakhand"
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

将条件放入subQuery内

$withCoutry = $registrationFieldObj->country == true;
$withState = $registrationFieldObj->state == true;
$userDetails->with(['userMeta'=>function($query) use ($withCountry, $withState) {
    $withArray = [];
    if ($withCountry) {
        $withArray['CountryDetails'] = function($query2){
            $query2->select('id','country_name');
        } 
    }
    if ($withState) {
        $withArray['stateDetails'] = function($query2){
            $query2->select('id','country_id','state_name');
        } 
    }
    $query->with($withArray);
}]);

或者如果您摆脱了其中的select部分

$withCoutry = $registrationFieldObj->country == true; //just to make it simpler
$withState = $registrationFieldObj->state == true;

$withRelation = ['userMeta'];
if ($withCountry) {$withRelation[] = 'userMeta.CountryDetails';}
if ($withState) {$withRelation[] = 'userMeta.stateDetails';}

$userDetails->with($withRelation)->get();

答案 1 :(得分:0)

有很多条件选择 你可以试试这个

if($registrationFieldObj->has('country')){
   //Your Logic
}

if(!empty($registrationFieldObj){
  //Your Logic
}

if(!$registrationFieldObj->isEmpty()){
 //Your Logic
}

希望这会有所帮助

相关问题