File Upload
I am using the below code to upload the file in STORE (Controller) and is is working fine but the same code is not working in UPDATE.
Can any one please help me, what may the reason.
$filename = '';
if ($request->hasFile('th_attach')) {
$file = $request->file('th_attach');
$ext = $file->getClientOriginalExtension();
$filename = date('YmdHis') . rand(1, 99999) . '.' . $ext;
$file->storeAs('public/categories', $filename);
}
$account->th_attach = $filename;
Thanks
The file upload part seems OK (with the exception that you're not deleting the previous image assigned to the record).
Stupid question, but are you saving the record after assigning $account->th_attach = $filename;?
Can we see the rest of the controller?
@guybrush_threepwood
Thanks for your reply. The reason for saving the file name in the table is to retrieve the file from the blade.
public function update(Request $request, Account $account, Item $item)
{
$filename = '';
if ($request->hasFile('th_attach')) {
$file = $request->file('th_attach');
$ext = $file->getClientOriginalExtension();
$filename = date('YmdHis') . rand(1, 99999) . '.' . $ext;
$file->storeAs('public/categories', $filename);
}
$account->th_attach = $filename;
$account->th_supp_name = $request->th_supp_name;
$account->th_supp_contact = $request->th_supp_contact;
$date = Carbon::createFromFormat('d-m-Y', $request->th_bill_dt);
$account->th_bill_dt = $date;
$account->th_bill_no = $request->th_bill_no;
$account->th_bill_amt = $request->th_bill_amt;
$account->th_item_type = $request->th_item_type;
$account->th_purpose = $request->th_purpose;
$account->save();
return redirect()->route('admin.accounts.index')->with('info', 'Transaction updated successfully!');
}
index.blade.php
<a href="{{ route('admin.accounts.edit',$c->th_tran_no) }}">
<i class="fa fa-edit"></i>
</a>
No problem. A couple of questions:
- Are you seeing any errors when doing the update?
- Are the other fields being updated (ie:
th_supp_name)?
- Is the file being created in the storage folder?
Try dd() step by step to have a better idea of what's going on:
dd($request->hasFile('th_attach'));
dd($request->file('th_attach'));
dd($file->storeAs('public/categories', $filename));
dd($filename);
Please or to participate in this conversation.