0

for make an api to return all images of the doctor

$images = Doctor::select('image')->get();

so the response is like the

`[
  { image: 111.png },
  { image: 222.png },
  { image: 333.png }
 ]`

then i return the images

return $images

I want to append the path to the image before return it like

mypath/222.png

i can do this with a for loop put i thank that made a more unnecessary complexity

so is their a way to make it in the eloquent

1
  • Look for eloquent map Commented May 6, 2018 at 13:16

1 Answer 1

1

If that's how you want to get the image column every time then accessor is the best option. In your Doctor model:

public function getImageAttribute($value){
    return 'mypath/'.$value;
}

If you want it only in this place then you can use map:

$images = Doctor::select('image')->get()->map(function($doctor){
    $doctor->image = 'mypath/'.$doctor->image;
    return $doctor;
});

and you will have your desired result.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.