Tuesday, May 15, 2018

Laravel keeps pulling back all relations despite using `with()` - development

Laravel keeps pulling back all relations despite using `with()`

For some bizarre reason my Laravel 5.6 app continues to return a User object with all of its relations.

My query in the Api/UserController:

    public function show($user_id)
    {
        return User::with('meta', 'roles')->find($user_id);
    }

The response:

{
    "id": 1,
    "name": "Admin",
    "email": "admin@example.com",
    "company_id": 1,
    "meta": {
        "id": 1,
        "user_id": 1,
        "laptop": 0,
        "mobile": 0,
        "created_at": "2018-03-07 14:58:41",
        "updated_at": "2018-04-06 16:13:10"
    },
    "roles": [
        {
            "id": 2,
            "name": "admin",
            "label": "Admin",
            "permissions": null,
            "pivot": {
                "user_id": 1,
                "role_id": 2
            }
        }
    ],
    "company": {
        "id": 1,
        "name": "Company",
        "active": 1,
        "created_at": "2018-04-12 15:06:01",
        "updated_at": "2018-05-15 11:20:15",
        "is_max_user_limit_reached": true
    }
}

The route (inside routes/api.php):

Route::group(['middleware' => 'auth:api'], function () {
    Route::resource('/users', 'ApiUserController', ['as' => 'api']);
});

User model:

        namespace AppModels;

        use AppModelsRole;

        class User extends Authenticatable implements HasMedia
        {
            use HasApiTokens, Notifiable, Billable, HasMediaTrait;

            protected $table = 'users';

            protected $fillable = ['name', 'email', 'password', 'is_active', 'company_id', 'stripe_id', 'card_brand', 'card_last_four', 'trial_ends_at'];

            protected $hidden = ['password', 'remember_token','card_brand', 'card_last_four'];

            protected $appends = ['extra', 'is_staff_only', 'first_four_training_sections', 'is_free_tier', 'is_agency_tier', 'is_team_tier', 'is_enterprise_tier'];


            public static $rules = [
                // create rules
                'name' => 'required',
                'email' => 'required|email|unique:users'
            ];


        public function meta()
        {
            return $this->hasOne(UserMeta::class);
        }


        public function company()
        {
            return $this->belongsTo(Company::class, 'company_id')->where('active', 1);
        }


        public function roles()
        {
            return $this->belongsToMany(Role::class);
        }


 public function getExtraAttribute()
    {
        return [
            'roles' => [
                'cpo' =>  (int)$this->hasRole('cpo'),
                'ap' =>  (int)$this->hasRole('ap'),
                'cao' => (int)$this->hasRole('cao'),
            ]
        ];
    }


    public function getIsStaffOnlyAttribute()
    {
        if($this->roles->count() == 1 && $this->hasRole('staff')) {
            return true;
        }
        return false;
    }

    public function getIsFreeTierAttribute()
    {
       return $this->company->subscription_tier == 0;
    }

    public function getIsAgencyTierAttribute()
    {
        return $this->company->subscription_tier == 1;
    }


    public function getIsTeamTierAttribute()
    {
        return $this->company->subscription_tier == 2;
    }


    public function getIsEnterpriseTierAttribute()
    {
        return $this->company->subscription_tier == 3;
    }



    public function getFirstFourTrainingSectionsAttribute() {
        return UserTrainingSection::where('user_id', $this->id)->orderBy('id')->take(4)->get();
    }

}

This is very strange behavior. I am asking for only the roles and meta related data but it’s always returning every single relation on the User model.

Even if I try User::find($user_id); it will still return all the relations.

Anyone know what’s going on here?

I’m using Laravel 5.6 and PHP 7.2



from Laravel Questions and Answers https://laravelquestions.com/php/laravel-keeps-pulling-back-all-relations-despite-using-with/
via Lzo Media

No comments:

Post a Comment