Thursday, April 26, 2018

How to set append variable on condition in laravel model? - development

How to set append variable on condition in laravel model?

I am doing project in Laravel. I have model named ‘User.php’ and here I want to set $appends variable depends on condition.

user.php looks like

class User extends Authenticatable
{
protected $appends = ['avatar_photo'];

public function profile_attributes()
    {
        //return $this->hasMany(AppModelsAccessUserUserProfile::class);
        return $this->belongsToMany(AppModelsAccessUserUserProfileAttributes::class,config('access.user_attribute_values_table'),'user_id','user_attribute_id')->withPivot('value', 'active')->withTimestamps();
    }

/**
     * @return string
     */
    public function getAvatarPhotoAttribute()
    {
        $avatar = $this->profile_attributes()->where('user_attributes_master.id',7)->first()->avatar_path;
        return $avatar;

    }
}

Here I get avatar_path attribute to every record which is append variable I have set to profile.php model. How to get this to only ‘avatar’ col of profile.php. How can I set this appends variable.



from Laravel Questions and Answers https://laravelquestions.com/php/how-to-set-append-variable-on-condition-in-laravel-model/
via Lzo Media

No comments:

Post a Comment