Aris Putrawan
Aris Putrawan

Reputation: 3

How do I set my JSON image to url format?

i want to ask. how to set my json image to url format.

i have code to store my image like this :

public function store(Request $request)
    {

        if ($request->hasFile('objek_pict')) {
            $filePic   = $request->file('objek_pict');
            $extension = $filePic->getClientOriginalExtension();
            $fileName  = date('m-d-Y_', time()) . $request->nama_objek;
            // $filePic->move('/uluwatu_image/', $fileName . '.' . $extension, file_get_contents($request->file('objek_pict')->getRealPath()));
            $filePic->move('uluwatu_image/', $fileName . '.' . $extension);
        }

        $new_objek = new ObjekTable();
        $new_objek->nama_objek = $request->nama_objek;
        $new_objek->objek_lat = $request->objek_lat;
        $new_objek->objek_lng = $request->objek_lng;
        $new_objek->objek_pict = 'uluwatu_image/' . $fileName . '.' . $extension;
        $new_objek->objek_deskripsi = $request->objek_deskripsi;
        $new_objek->save();

        return redirect('masterdata')->with('success', 'Data Berhasil Ditambah');
    }

and this is how i get my json data public function getDatajson(Request $request) {

    $lokasi = ObjekTable::select('id_objek as id', 'nama_objek as name', 'objek_lat as latitude', 'objek_lng as longitude', 'objek_deskripsi as description', 'objek_pict as image_object')->get();
}

this my json data link : https://aruluwatu.000webhostapp.com/api/json

this the picture : https://i.sstatic.net/aff6L.jpg

I want to make my json image show the url path like this: https://aruluwatu.000webhostapp.com/uluwatu_image/04-15-2020_Rumah.jpg

*uluwatu_image/04-15-2020_Rumah.jpg (my saved image)

Upvotes: 0

Views: 114

Answers (1)

Ahmed Atoui
Ahmed Atoui

Reputation: 1556

you can do this by using Eloquent Accessors that allow you to define custom attributes

in model ObjekTable define append attribute :

protected $appends = ['image_url'];

then set value image_url attribute on same model :

public function getImageUrlAttribute()
{
   return url(Storage::url( $this->objek_pict ));
}

now you get new attribute image_url on model instant contain the full image url

Upvotes: 1

Related Questions