I create a resource for NewProgram
model, it works for READ, CREATE AND DELETE, but not for UPDATE, the problem is there is a file need to update too, here's my store
function for example
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$program = new NewProgram;
$program->name = $request->name ?? '';
$program->description = $request->description ?? '';
$program->date_from = $request->date_from ?? '';
$program->date_to = $request->date_to ?? '';
$program->location = $request->location ?? '';
$program->organizer = $request->organizer ?? '';
if ($request->hasFile('image')) {
$attachment = $this->uploadAttachment($request->file('image'));
$program->image_url = route('image', $attachment->uuid);
}
$program->save();
return response()->json($program);
}
Yup, that's for store
function and it works perfectly using postman, but here's the problem, I can update the data from NewProgram, but not for the file because the update
function only receives a request from x-www-form-urlencoded
not like store
that receive from form-data
, here's my code
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$program = NewProgram::find($id);
$program->name = $request->name ?? '';
$program->description = $request->description ?? '';
$program->date_from = $request->date_from ?? '';
$program->date_to = $request->date_to ?? '';
$program->location = $request->location ?? '';
$program->organizer = $request->organizer ?? '';
$program->image_url = $request->image_url ?? '';
$program->save();
return response()->json($program);
}
here's screenshot from postman
How to fix this? I want to make update
function also update the file/attachment, not the string