-1

I am getting data from database and i have a field profile_pic and i am saving image name in my database my response looks like:

"basicInfo": {
    "id": 205,
    "first_name": "new name",
    "middle_name": "middle",
    "profile_pic": "3q4Vs8iHdY.png",            
}

As you can see profile_pic i am getting image name and there folder public/profile_images where my images is storing and when i hit get Api I GOT following response with image name in profile_pic. but i want to get full path in my response by attaching path from public/profile_images:

My code where I got this response

$userBasicInfo = $this->userBasicInfo->where('user_id', $user_id)->first();

This gets all data but i want to attach image path name in my profile_pic as well How I can do that? I am stuck here

class UserBasicInfo extends Model
{
    use SoftDeletes;

    const DELETED_AT = 'deletedAt';
    protected $table = "user_basic_info";

    protected $fillable = [
        'first_name','city','state','zip','social_security','middle_name', 'last_name', 'profile_pic', 'date_of_birth', 'gender', 'area_id', 'user_id', 'created_by', 'updated_by', 'created_at', 'deletedAt','title','cell_no','address','work_phone','fax','extension','primary_facility','affiliated_facility','employed_by','emergency_phone','designation','department','employment_type','biography','hiring_date','from','to'
    ];

    protected $hidden = ['deletedAt'];

    function user() {
        return $this->belongsTo('App\User', 'id', 'user_id');
    }

    public function getFromAttribute($value){
        $createdAt= Carbon::parse($value);
        return $createdAt->toIso8601String();
    }

    public function getToAttribute($value){
        $createdAt= Carbon::parse($value);
        return $createdAt->toIso8601String();
    }
}


if (!empty($userBasicInfo->profile_pic)){
    $deleteImage =$userBasicInfo->profile_pic;
    unlink(public_path('profile_images').'/'.$deleteImage);
}
Thomas Van der Veen
  • 3,136
  • 2
  • 21
  • 36
syed1234
  • 761
  • 5
  • 12
  • 30

1 Answers1

2

You can use accessor in your UserBasicInfo model :

public function getProfilePicAttribute($value)
{
    return 'path/to/image' . $value;
}

Guide to Accessors from laravel documentation:

https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor

Hazem Emad
  • 419
  • 1
  • 3
  • 8
  • I have shared my model of userBasicinfo can you look and add logic there your help will apprecaited – syed1234 Jul 22 '19 at 15:18
  • just add the function i wrote above among the others, it should work this way – Hazem Emad Jul 22 '19 at 15:35
  • issue is solved but there is problem i am using same model for profile image upload and same model will be to get image path when i get it shows path correctly but when i upload it says "unlink(E:\\xampp\\htdocs\\feature-master\\public\\profile_images\/http:\/\/localhost\/feature-master\/public\/profile_images\/jwztUw4hbv.png): No such file or directory" – syed1234 Jul 22 '19 at 15:37
  • because i delete previous once on upload – syed1234 Jul 22 '19 at 15:38