Reputation: 315
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...
Upvotes: 0
Views: 583
Reputation: 111859
This is because you save record like this:
$user = News::create($input);
so you don't save $imgfile
in database.
You can add after:
$success['img'] = $imgfile;
the following line to update record
$user->update(['img' => $imgfile]);
or you can change the order of your code to create News
record later with valid img
field already.
Also keep in mind you are using wrong variable names. There is no point to have $user
variable that holds News
model.
Upvotes: 1
Reputation: 3772
As Marcin has stated above it's because you're not saving imgfile into the database.
I would also consider refactoring some of your code so that the add method isn't so verbose. Consider something like the following code block. Please note no use statements have been used an no try/catch has been implemented.
I have also found that it is generally better to give fuller variable and property names. Such as fname I would call first_name/firstName depending on context.
<?php
class ApiController
{
/**
* Add news item
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function add(Request $request)
{
$validator = $this->validateData($request);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 422);
}
$news = News::create($request->all());
if ($news) {
$success['userid'] = $news->userid;
$success['fname'] = $news->fname;
$success['lname'] = $news->lname;
$success['img'] = $this->uploadImage($news);
$news->update(['img' => $success['img']]);
return response()->json(['success'=>$success], $this->successStatus);
}
return response()->json(['error'=>'An Error occurred when creating the news item'], 422);
}
/**
* Upload Image and return file name
*
* @param $news
* @return string
*/
protected function uploadImage($news)
{
$image = $news->img;
$image = str_replace('data:image/jpg;base64,', '', $image);
$image = str_replace(' ', '+', $image);
$data = base64_decode($image);
$file = 'uploads/' . uniqid() . '.jpg';
file_put_contents($file, $data);
return $file;
}
/**
* Validate inbound request
*
* @param $request
*/
protected function validateData($request)
{
Validator::make($request->all(), [
'userid' => 'required',
'fname' => 'required',
'lname' => 'required',
'img' => 'required',
]);
}
}
Upvotes: 1