mercredi 4 mars 2020

Fetch image as a url from mysql database in laravel Api

I want to fetch image from database as a url like "http://127.0.0.1:8000/uploads/category/image.png" but the result I am getting is file the name not the url.

this is model class

''' class CategoryModel extends Model { protected $table="tb_category"; public $timestamps = false;

   protected $fillable = [
   'id',
   'name',
   'image'
   ];
   }

'''

this is my controller class ''' class CategoryController extends Controller { // public function viewPage(){ return view('category'); }

public function saveCategory(Request $request){
    $category = new CategoryModel();
    $category->name=$request->input('name');

    if($request->hasfile('image')){
        $file=$request->file('image');
        $extension = $file->getClientOriginalExtension();
        $filename=time().'.'.$extension;
        //$headers = array('Content-Type' => 'application/octet-stream');
        $file->move('uploads/category/',$filename);
        $category->image =$filename;
        $category->_image =$filename;



     }
     else{
        return $request;
        $category->image='';
        $category->_image=null;
     }

        $category->save();
        return view('category')->with('category',$category);

    }


    public function getCategories(){
    $res = CategoryModel::get(
        ['id','name','image'
     ]
     );   
     return response()->json($res,200);
     }
     }

'''


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire