Skip to content
Advertisement

How can I overwrite my file images in laravel

I would like to ask how can i overwrite/update my images inside the form and store in database? I tried doing it but it is not working, i get the id correct but when i upload new image and text, it is not updating my old file. Below i attached my code.

company controller

 public function update(Request $request, $id)

{

    $this->validate($request, [
        'company_logo' => 'required|image|max:2048',
        'company' => 'required',

    ]);
//    dd(123);
    $company = Company::find($id)->first();
    // dd($company);


    // $company = Company::where(['companies.id' => $id])->first();
    if($request->file != '')
    {
        $path = public_path().'/uploads/images/';
        $file = $request->file;
        $filename = $file->getClientOriginalName();
        $file->move($path, $filename);

        $company->update(['file' => $filename]);
        return redirect()->route('company.index');
        $company->file = $path.$filename;
    }

    $company->company = $request->company;
    $company->save();

    return redirect()->route('company.index');

}
public function insert_logo(Request $request)
{
    $company = new Company();

    $company->company    = request('company');
    $company->company_logo    = request('company_logo|file|image|max:10,485,760');


    // uploading image file to database
   if
   ($file = $request->hasFile('company_logo')) {
       $file = $request->file('company_logo');

       $filename = time() . '.' .$file->getClientOriginalName();
       $destinationPath = public_path('/uploads/images/');

       $file->move($destinationPath, $filename);
       $company->company_logo = $filename;
   }
   $company->save();
   return back();

}

edit modal for company

 <div class="modal" id="modal-edit">
<div class="modal__content">

    <div class="intro-y flex items-center p-5">
        <h2 class="text-lg font-medium mr-auto">
            Edit Company
        </h2>
    </div>

    <form action="{{ route('company.update', $company->id) }}" method="post">
        @csrf
        @method('PUT')
        
        <div class="intro-y box p-5">
            <div>
                <label>Company Logo</label>
                <div class="mt-2">
                    <div class="mt-2">
                        <div class="row mt-4">
                            <div class="col-md-8">
                                <input type="file" name="company_logo"/>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="mt-3">
                    <input type="text" placeholder="Company Name" name="company"
                    class="input w-full border col-span-4 form-control tail-select" value="{{ $company->company}}">
                    
                </div>

                <div class="text-right mt-5">
                    <button type="button" data-dismiss="modal"
                    class="button w-24 border text-gray-700 dark:border-dark-5 dark:text-gray-300 mr-1">Cancel</button>
                    <button type="submit" class="button w-24 bg-theme-1 text-white">Update</button>
                </div>
            </div>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript"> 
$('#modal-edit').modal('show')</script>

I might be wrong inside the function but I am not sure where it went wrong. so far only the company name is showing, but still even if I change my company name, and click button update, it wont overwrite my old one

Advertisement

Answer

Form Tag missed enctype="multipart/form-data"

<form enctype="multipart/form-data" action="{{ route('company.update', $company->id) }}" method="post">

once you called find then no need first so remove ->first()

 $company = Company::find($id);

It should be $request->company_logo not $request->file

  $company = Company::find($id);
     if($request->hasFile('company_logo'))
        {
            $path = public_path().'/uploads/images/';
            $file = $request->company_logo;
            $filename = $file->getClientOriginalName();
            $file->move($path, $filename);
    
            $company->company_logo =$path.$filename;
           
         
        }
       $company->company = $request->company;
       $company->save();
       return redirect()->route('company.index');

Usually full path is not saved in db.Better only save filename so that you can keep base path in filesystem configuration.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement