jeudi 22 juin 2017

Laravel 5.4 replace file if exist

I need to solve a small inconvenience that I am presenting with the update of an image when modifying the associated registry.

When I modify the registry I want to replace the associated file that is in the directory.

This is my table structure:

Schema::create('institutions', function(Blueprint $table)
        {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('initials')->nullable();
            $table->string('description')->nullable();
            $table->string('avatar')->default('default.jpg');
            $table->timestamps();
        });

This is my update method on my controller:

public function update(Request $request, $id)
    {
        //

        $institution = $this->institution->find($id);

        try
        {
            $institution->update($request->all());

            if($request->hasFile('avatar')){
                $avatar = $request->file('avatar');
                $filename = time() . '.' . $avatar->getClientOriginalExtension();
                Image::make($avatar)->resize(250, 205)->save( public_path('uploads/institutions/' . $filename ) );

                $institution->avatar = $filename;
                $institution->save();
            }

            $updated = $institution;

            $message = flash('Institución actualizada correctamente!!!', 'success');

            return redirect()->route('instituciones.index')->with('message', $message);    
        }       

        catch(\Illuminate\Database\QueryException $e)
        { 
            $message = flash('La institución no se actualizó correctamente!!!', 'danger');

            return redirect()->route('institutions.create')->with('message', $message); 
        }   

    }

I have tried different methods but I have not succeeded.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tTXNJX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire