dimanche 24 décembre 2017

Laravel 5 json response image path is not stored in mysql database

I am sending base64 image along with some form data from postman to laravel 5 with mysql. The code works well and returns the success json as well. All values from json is stored in mysql but the json image path is not stored in database, instead some code is stored in databse.

My actual json response in postman is =>

    {"success":{"userid":"4","fname":"s","lname":"s","img":"uploads\/5a3f6218a1ed0.jpg"}} 

All above json values are stored in databse but the img json path is not stored, instead following data is stored in database.

   /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQEBw7KCIoOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs 

My Laravel API code is

public function add(Request $request)
{
     $validator = Validator::make($request->all(), [             
        'userid'   => 'required', 
        'fname' => 'required', 
        'lname'  => 'required',            
         'img'   => 'required',    
    ]);

    if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);        
    }
       $input = $request->all();
       $user = News::create($input); 
       $success['userid'] =  $user->userid;
      $success['fname'] =  $user->fname;
      $success['lname'] =  $user->lname;        

   if ($user->img)
      {
       $img2 = $user->img;
      $img3 = str_replace('data:image/jpg;base64,', '', $img2);
      $img3 = str_replace(' ', '+',  $img3);
      $data = base64_decode($img3);
      $file = 'uploads/' .  uniqid() . '.jpg'; 
      file_put_contents($file, $data);   
         $imgfile = $file; 
         $success['img']  =  $imgfile;
       }  
    return response()->json(['success'=>$success], $this->successStatus);
}

What actually i am missing...



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kOdHU0
via IFTTT

Aucun commentaire:

Enregistrer un commentaire