1

I want to modify some values of my query result for API call. here's my query

public function modules()
{
    Module::select('id','name','image')->get()->toJson();
}

so I want to modify image value, add full path to it. I added accessor

public function getImageAttribute($image)
{
    return asset($image);
}

and it works fine, but the problem is now it will be affecting this column every time i try to retrieve it. is there way to use this accessor(getImageAttribute) only for modules method?

1 Answer 1

3

You could get rid of the accessor and update the image path inside the modules method.

public function modules()
{
    return Module::select('id', 'name', 'image')->get()->map(function ($module) {
        $module->image = asset($module->image);
        return $module;
    })->toJson();
}
Sign up to request clarification or add additional context in comments.

2 Comments

just couple of minutes, and I will be able to accept it
much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.