Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mozew's avatar
Level 6

How to update and edit a image in laravel 5.8

I have a table called Services, now this table has the following -

-id -title -body -image -slug -timestamps

And in administrator, services page I created for Add, Edit, Delete it.

My problem is edit and update a image. A strange problem is that, when I change the of image to aaaaa field in the service table (database), Nothing happens. should happen. because I changed rename of image to aaaaa.

web.php

Route::resource('services', 'ServiceController');

ServiceController.php

public function edit(Service $service)
{
    return view('Admin.services.edit', compact('service'));
}

public function update(Request $request, Service $service)
{
    $service->title = $request->title;
    $service->body = $request->body;

    if($request->hasFile('image')) {
        $image = $request->file('image');
        $filename = $image->getClientOriginalName();
        $image->move(public_path('images/services'), $filename);
        $service->image = $request->file('image')->getClientOriginalName();
    }

    $service->update();

    return redirect()->route('services.index');
}

edit.blade.php

<form class="form-horizontal" action="{{ route('services.update', $service->id) }}" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
    {{ method_field('PATCH') }}
    @include('Admin.layouts.errors')
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" id="title" name="title" placeholder="Title" value="{{ $service->title ? : old('title') }}">
    </div>
    <div class="form-group">
        <label for="body">Body</label>
        <textarea class="form-control" rows="10" id="body" name="body" placeholder="Body">{{ $service->body ? : old('body') }}</textarea>
    </div>
    <div class="form-group">
        <label for="image">Image</label>
        <div class="custom-file">
            <input type="file" class="custom-file-input" id="image" name="image">
            <label class="custom-file-label" for="image">Image</label>
        </div>
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary">Save</button>
    </div>
</form>

Service.php

protected $fillable = [
    'title',
    'body',
    'image',
    'slug',
];

I even changed the controller in the update method as follows, nothing happened.

$service->save();
0 likes
27 replies
Snapey's avatar

You must use save() not update()

You should check that you are being given the correct Service model

$fillable is not relevant because you are not doing mass assignment

dd($service) and see if it is the correct model and that the image name has been set

Does the file get updated in the file system?

PaulCatalin97's avatar

this worked for me

class ImageUploadController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {
        $profileimage = ProfileEmployee::where('uid', Auth::user()->id)->first();
        return view('imageupload', compact('profileimage'));
    }

    public function update(Request $request)
    {
        $request->validate([
            'image' => 'image|mimes:jpeg,png,jpg|max:2048'
        ]);
        $employee = ProfileEmployee::where('uid', Auth::user()->id)->first();

        if ($request->hasfile('image')){
            $file = $request->file('image');
            $extension = $file->getClientOriginalExtension();
            $filename = md5(time()).'.'.$extension;
            $file->move(public_path().'\imagineprofil',$filename);
            $employee->image=$filename;
        } else {
            return $request;
            $employee->image='';
        }

        if($employee->save()){
            return redirect()->route('imageupload')->withSuccess('S-a incarcat cu success!');
        }else{
            return redirect()->route('imageupload')->withDanger('Nu s-a incarcat! A aparut o eroare.');
        }
    }

}

profileemployee is my table

PaulCatalin97's avatar

routes

Route::get('/imageupload', 'Auth\ImageUploadController@index')->name('imageupload');
Route::post('/imageuploadupdate', 'Auth\ImageUploadController@update')->name('imageupload.update');

mozew's avatar
Level 6

@SNAPEY - Does the file get updated in the file system? No.

Everything is okay $fillable,......

I writed in Controller

public function update(Request $request, Service $service)
{
    dd($service);
}

I see this message

Service {#336 ▼
  #fillable: array:4 [▶]
  #connection: "mysql"
  #table: "services"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [▼
    "id" => 5
    "user_id" => 1
    "title" => "laptop"
    "image" => "ghali.png"
    "body" => "<p>A laptop computer (also shortened to just laptop; or called a notebook or notebook computer) is a small, portable personal computer (PC) with a &quot;clamshe ▶"
    "slug" => "پرده-شویی"
    "created_at" => "2019-05-18 07:32:02"
    "updated_at" => "2019-05-18 20:14:27"
  ]
  #original: array:8 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

Service {#336 ▼
  #fillable: array:4 [▶]
  #connection: "mysql"
  #table: "services"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [▼
    "id" => 5
    "user_id" => 1
    "title" => "laptop"
    "image" => "ghali.png"
    "body" => "<p>A laptop computer (also shortened to just laptop; or called a notebook or notebook computer) is a small, portable personal computer (PC) with a &quot;clamshe ▶"
    "slug" => "پرده-شویی"
    "created_at" => "2019-05-18 07:32:02"
    "updated_at" => "2019-05-18 20:14:27"
  ]
  #original: array:8 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
Service {#336 ▼
  #fillable: array:4 [▶]
  #connection: "mysql"
  #table: "services"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [▼
    "id" => 5
    "user_id" => 1
    "title" => "laptop"
    "image" => "ghali.png"
    "body" => "<p>A laptop computer (also shortened to just laptop; or called a notebook or notebook computer) is a small, portable personal computer (PC) with a &quot;clamshe ▶"
    "slug" => "پرده-شویی"
    "created_at" => "2019-05-18 07:32:02"
    "updated_at" => "2019-05-18 20:14:27"
  ]
  #original: array:8 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

This is the previous photo. My new photo is 1.jpg.

Snapey's avatar

Change controller to

public function update(Request $request, Service $service)
{
    dd($request->image);
}

What do you get?

Snapey's avatar

so nothing in this controller is the problem, you are not receiving the image

What does your controller say at the top where you use the request class

mozew's avatar
Level 6

@SNAPEY - No. In the edit page, after I see null, after upload my image.

Snapey's avatar

show the top of your controller

mozew's avatar
Level 6

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ServiceRequest;
use App\Service;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
mozew's avatar
Level 6

How do you do it to update the image?

Snapey's avatar

you cannot update the image until you receive the image

we need to see why the image is not being sent to your application (assuming you are actually attaching an image)

Snapey's avatar

can you use the network toolsin your browser to see if the image is being sent?

mozew's avatar
Level 6

I just upload the image with the .jpg extension.

Snapey's avatar

do the same but click on the params section

Cronix's avatar

Strange, that shows you're sending the file input with the name images not image. How did that change? Your original post shows you sending it as image.

No wonder $request->image is null. It's $request->images, I guess.........

mozew's avatar
Level 6

I replaced image to images now. but my problem did not solve.

Snapey's avatar

you must be systematic in your testing. Don't just change one thing and hope all your problems are fixed. It could be a combination of issues.

So, why does your form say image but your request contain images ?

Are you showing the right code?

babonday's avatar

Anyone got the link to how to do this on a laracasts video?

jlrdw's avatar

Why would a video be needed to Simply update an image, as said, you would upload a new image just like you did the original and just delete the not needed image.

Please or to participate in this conversation.