bhhussain's avatar

File upload not saving the file name in the database field

This below code is not updating the uploaded file name while updating but the same code we are using for creating new record it is working.

   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;       

       $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!');;
    }

Edit view

<div class="form-group">
  <div class = "row">
  
  <label class = "col-lg-1" for="">Attach Bill</label>
  <div class = "col-md-6">    
  <input type="file" id="validationCustom01" name="th_attach">
  <div class = "clear-fix"></div>
  </div>     
  </div>
  </div>
  

Can any one please help me?

Thank you

0 likes
5 replies
bugsysha's avatar

It looks like you haven't uploaded a file when you've tried it and then it does go into if statement if( $request->hasFile('th_attach')) and that is why $filename remains empty string as you have it defined $filename='';.

For example change $filename=''; to $filename='something'; and see if with the same flow (without uploading a file) you get $account->th_attach populated and saved in the database.

bhhussain's avatar

@bugsysha Thank you very much for your reply.. I tried already as you said.. It is saving the value.. example.. it is inserting the value 'something' into the table but it is not inserting the document

bugsysha's avatar

So when you create this resource everything works as it should, but now when you reuse that logic for updating it is not working? Is only the name problem or file uploading also?

bhhussain's avatar

Yes... you are correct.. It is not even uploading the file and not updating the file name in the table.

bugsysha's avatar

Then you need to post here contents of blade file responsible for editing that resource with all code related to serving that edit page and storing it.

Please or to participate in this conversation.