I have this function that will update the data as showing below:
public function update(Request $request, OutstandingPayment $outstandingPayment)
{
$request->validate([
'file' => 'required|mimes:jpg,jpeg,png,csv,txt,xlx,xls,pdf|max:2048',
'payment_voucher_file_ref_no' => 'required|string',
'payment_voucher_date' => 'required|string',
]);
if ($request->hasFile('file')) {
$file_name = time().'_'.$request->file->getClientOriginalName();
$file_path = $request->file('file')->move(public_path('uploads/outstandingPayment'), $file_name);
$request->payment_voucher_supporting_doc = time().'_'.$request->file->getClientOriginalName();
$request->path = '/storage/' . $file_path;
}
$postData = [
'payment_voucher_file_ref_no' => $request->payment_voucher_file_ref_no,
'payment_voucher_date' => $request->payment_voucher_date,
'payment_voucher_supporting_doc' => $request->payment_voucher_supporting_doc,
'path' => $request->path,
];
$outstandingPayment->update($postData);
}
The above function is working fine but only if i uploaded new file
Note: "File" is -> "file" is "payment_voucher_supporting_doc" i am passing it in the request as "file" and place it in "payment_voucher_supporting_doc"
But in my case now, if there are no changes on "file" i want the current one to stay the same
I tried to add "else" if there is no file and assign "payment_voucher_supporting_doc" to outstandingPayment->payment_voucher_supporting_doc" but the response show that still "file" is required
How can i achieve that?
CodePudding user response:
I don't find best ways on your code about whether your upload file had changed or not.
For example, can you try to set guard method
before update your file?
like this:
public function update(Request $request, OutstandingPayment $outstandingPayment)
{
$request->validate([
'file' => 'required|mimes:jpg,jpeg,png,csv,txt,xlx,xls,pdf|max:2048',
'payment_voucher_file_ref_no' => 'required|string',
'payment_voucher_date' => 'required|string',
]);
// all validate has passed,so please check whether your file contents has changed or not
// file name change is all contents has changed?
$isSameFile = ( // true or false
$request->hasFile('file')
&&
preg_match('/'.$request->file->getClientOriginalName().'/',$outstandingPayment->payment_voucher_supporting_doc)
);
// if isSameFile is true, don't need to update file status?
if ($isSameFile) return;
// if you don't need above guard method, process is under following.
$file_name = ($isSameFile) ? $outstandingPayment->payment_voucher_supporting_doc : time() . '_' . $request->file->getClientOriginalName();
$request->payment_voucher_supporting_doc = $file_name;
$request->path = ($isSameFile) ? $outstandingPayment->path : '/storage/' . $request->file('file')->move(public_path('uploads/outstandingPayment'), $file_name);
// if ($request->hasFile('file')) {
// $file_name = time() . '_' . $request->file->getClientOriginalName();
// $file_path = $request->file('file')->move(public_path('uploads/outstandingPayment'), $file_name);
// $request->payment_voucher_supporting_doc = time() . '_' . $request->file->getClientOriginalName();
// $request->path = '/storage/' . $file_path;
// }
$postData = [
'payment_voucher_file_ref_no' => $request->payment_voucher_file_ref_no,
'payment_voucher_date' => $request->payment_voucher_date, // is this belong with upload action ?
'payment_voucher_supporting_doc' => $request->payment_voucher_supporting_doc,
'path' => $request->path,
];
$outstandingPayment->update($postData);
}
how do you check file is newest or not ? name or contents detail?
ps: you use time()
method some places. if your function
processing speed is slow, your file name will be different like 160000_hoge.png and 160003_hoge.png.
so, I recommend to fix that. :)
CodePudding user response:
Just make your validation nullable
and then put every field that you want update with file in if block:
a hypothetical example(because your code is so long and I use mobile to type this answer): I want update user's profile and name but i want profile will be update if there is a picture in my request:
public function update(Request $request, User $user){
$request->validate([
'picture' => 'nullable|mimes:jpeg',
'name'=>'required|max:255'
]);
if($request->hasFile('picture')){
/* Upload file */
$user->profile= 'uploaded picture path';
}
$user->name = $request->name;
$user->update();
}
In my example, profile field will be updated if I upload a picture, otherwise just user's name will be updated