I want to create an API for a report store and there is an option to add more than one violator.
I have a problem, when I add a violator, in POSTMAN it displays the error "Call to a member function getClientOriginalName() on null".
How to fix it? or how to properly create an API with attach data one to many relationship ?
the following, the code that I have made;
Controller.php
public function store(ReportRequest $request)
{
try {
$report = Report::create([
'category_id' => $request->category_id,
'user_id' => Auth::user()->id,
'title' => $request->title,
'description' => $request->description,
'incident_date' => $request->incident_date,
'injured_victims' => $request->injured_victims,
'survivors' => $request->survivors,
'dead_victims' => $request->dead_victims,
'location' => $request->location,
'latitude' => $request->latitude,
'longitude' => $request->longitude
]);
$data[] = array(
'violator_photo' => $request->file('violator_photo'),
'violator_name' => $request->violator_name,
'violator_age' => $request->violator_age,
'violator_phone' => $request->violator_phone
);
foreach ($data as $key ) {
$photo = $key['violator_photo'];
$fileName = time().'_'.$photo->getClientOriginalName();
$filePath = $photo->storeAs('images/violators', $fileName, 'public');
Violator::create([
'report_id' => $report->id,
'name' => $key['violator_name'],
'photo' => $filePath,
'age' => $key['violator_age'],
'phone' => $key['violator_phone'],
]);
}
return response()->json([
'status' => '200',
'message' => 'success',
'data' => $report,
]);
} catch (\Exception$err) {
return $this->respondInternalError($err->getMessage());
}
}
CodePudding user response:
try it with this code
public function store(ReportRequest $request)
{
try {
$report = Report::create([
'category_id' => $request->category_id,
'user_id' => Auth::user()->id,
'title' => $request->title,
'description' => $request->description,
'incident_date' => $request->incident_date,
'injured_victims' => $request->injured_victims,
'survivors' => $request->survivors,
'dead_victims' => $request->dead_victims,
'location' => $request->location,
'latitude' => $request->latitude,
'longitude' => $request->longitude
]);
if($report){
if($request->hasFile('violator_photo')) {
$violator_photo=$request->file('violator_photo');
$fileName = time().'_'.$violator_photo->getClientOriginalName();
$filePath = $violator_photo->storeAs('images/violators', $fileName, 'public');
}
Violator::create([
'report_id' => $report->id,
'name' => $request->violator_name,
'photo' => $filePath?:'',
'age' => $request->violator_age,
'phone' => $request->violator_phone,
]);
}
return response()->json([
'status' => '200',
'message' => 'success',
'data' => $report,
]);
} catch (\Exception$err) {
return $this->respondInternalError($err->getMessage());
}
}
CodePudding user response:
The error getClientOriginalName(), I think that is because the "violator_photo" is not a file, but it's a string/text
so, if you want to send file on a json request, the simple way is to convert the file to base64
and on the controller, you can decode it, and save the file
here is the example how to decode it, there one thing you need to check for the base64. Some of base64 encode, add extra information about the file type, for example if the file is image png, the data is look like this
data:image/png;base64,iVBORw0KGg..rest of base64
so if the data look like this you need to remove the information and just get the base64 simply you can do it whit this code
$str ="base64 code";
$converted = substr($str, strpos($str,',') 1);
$image = base64_decode($base64_str);
$filename = "the file name.png"; // or you can get the extension name by the information the include on the first of base64
$path = "images/violators/";
Storage::disk('your disk')->put($path.$filename, $image, 'public');