Ahmed aljablai
Ahmed aljablai

Reputation: 11

Return URL image in Laravel API

I need to return URL path and name image from database.

This my call method to get data from database:

public function index(){
    $rav=Rav::select('id','image','nameravs')->get('image');
    return $this->sendResponse($rav->toArray(),'rav show is succesfuly');
}

Output data it look like this:

{ "id": 88, "image": "1579781806.png", "nameravs": "asdad" },

But I want to return image with path URL like this:

"image": http://127.0.0.1:8000/images_ravs/1579781806.png

Upvotes: 1

Views: 5874

Answers (6)

Jahongir Tursunboyev
Jahongir Tursunboyev

Reputation: 366

In the Model you can create getAttribute:

   public function getImageAttribute()
    {
      return $this->attributes['image'] ? URL::to('/uploads/' . $this->attributes['image']) : null; 
    }

Or you can create custom casts file:

    protected $casts = [
        'image' => ImageCast::class,
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

Upvotes: 0

Rachid Loukili
Rachid Loukili

Reputation: 845

Define An Accessor in your model like this example :

public function getImageAttribute($value)
{
    return env('APP_URL') . Storage::url($value);
}

Upvotes: 2

Edwin Krause
Edwin Krause

Reputation: 1806

I would do something like this: Try to use as many od Laravel's built-in functions as possible. In most cases you don't need to reinvent the wheel.

public function index(){
   $ravs = Rav::select('id','image','nameravs')->get();
   $results = [];

   foreach($ravs as $rav){
      $rav->image = env('APP_URL') . '/images_ravs/' . $rav->image;
      array_push($results, $rav);
   } 
   return response()->json($results);
 }

Upvotes: 0

gihandilanka
gihandilanka

Reputation: 615

You can use Eloquent Accessor

add the image url attribute in Rav.php Model like below. then you can access it any place.

    public function getImageUrlAttribute($value)
    {
        return env('APP_URL'). '/images_ravs/' . $this->image;
    }

And don't forget to Add Appends property inside the Model

/**
 * The accessors to append to the model's array form.
 *
 * @var array
 */
protected $appends = ['image_url'];

then you can access it like below.

$rav->image_url 

Upvotes: 7

Ohgodwhy
Ohgodwhy

Reputation: 50787

You can use selectRaw or select with DB::raw and just concat that path as a prepended string infront of your image column in your query:

Rav::select(DB::raw("id, nameravs, CONCAT('/images_ravs/', image) as image"))->get()->toArray();

No need to include the URL (unless for some reason you have some type of CDN in front of it) in which case you can just escape the string and add it if necessary.

Upvotes: 0

Burnie
Burnie

Reputation: 27

Try this:

public function index(){
    $rav = Rav::select('id','image','nameravs')->get('image');

    return $this->sendResponse(
        [
            'id' => $rav->id,
            'image' => URL::to('/').'/images_ravs/'.rav->image,
            'nameravs' => $rev->nameravs
        ],'rav show is succesfuly');

 }

Upvotes: 0

Related Questions