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

t9dev's avatar
Level 2

Update() function doesn't update the Image

I am creating a web-based app for Internship, and created a CRUD for companies info including company logo image, every thing works perfectly, until I try to edit the image for the company, it does not update the image_path on the DB if I tried to update the image it shows the alt for that img tag. Here is the Controller Code for the Update Function:

public function update(Request $request, $id)
    {
        $validated = $request->validate([
            'name' => 'required|max:255',
            'eco_sector' => 'required',
            'sector' => 'required',
            'email' => 'required',
            'phone_number' => 'required',
            'image' => 'mimes:jpg,png,jpeg|max:5048',
        ]);

        if ($companyImage = $request->file('image')) {

            $oldImageName = $request->old_image;
            $ImageName = time() . '-' . $request->name . '.' . $request->image->getClientOriginalExtension();
            $up_location = 'images/company/';
            $newImage = $up_location . $ImageName;
            $companyImage->move($up_location, $ImageName);

            unlink($oldImageName);

            company::find($id)->update([
                'name' => $request->name,
                'eco_sector' => $request->eco_sector,
                'sector' => $request->sector,
                'email' => $request->email,
                'phone_number' => $request->phone_number,
                'image_path' => $newImage,
                'updated_at' => Carbon::now(),
            ]);
            return Redirect()->route('company.index')->with('success', 'Company Record Updated Successfully');
        } else {

            company::find($id)->update([
                'name' => $request->name,
                'eco_sector' => $request->eco_sector,
                'sector' => $request->sector,
                'email' => $request->email,
                'phone_number' => $request->phone_number,
                'updated_at' => Carbon::now(),
            ]);
            return Redirect()->route('company.index')->with('success', 'Company Record Updated Successfully');
        }
    }

Blade View

<div class="d-flex flex-row mb-3"><img src="{{asset($data->image_path)}}" width="70" alt="Company Image">
                                    <div class="d-flex flex-column ml-2"><span>{{ $data->name }}</span><span class="text-black-50">{{ $data->eco_sector }}</span><span class="ratings"><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i><i class="fa fa-star"></i></span></div>
                                </div>

What am I doing Wrong ?

0 likes
31 replies
MohamedTammam's avatar

Make sure that you see the image in you project folders.

And it's better to use store method.

$image_path = $request->file('image')->store('images')

And in your view.

<img src="{{Storage::url($data->image_path)}}" width="70" alt="Company Image">

https://laravel.com/docs/9.x/filesystem

1 like
t9dev's avatar
Level 2

@MohamedTammam Will this work on Laravel 8.x ? Also, the store function will create a string, which will cause an error in the move() function.

MohamedTammam's avatar

@t9dev Yes it will work.

The store method is moving the file to the storage file with a unique name. It replaces that part for you.

$ImageName = time() . '-' . $request->name . '.' . $request->image->getClientOriginalExtension();
$up_location = 'images/company/';
$newImage = $up_location . $ImageName;
$companyImage->move($up_location, $ImageName);
t9dev's avatar
Level 2

@MohamedTammam when I update without the image, every thing is working, yet with the image it doesn't change the image_path on the DB, but the other data is updating.

MohamedTammam's avatar

@t9dev Okay, for update_at to be updated, You need to add that line into your model.

public $timestamps = true;

Please share you code again if you changed anything in it.

t9dev's avatar
Level 2

@MohamedTammam , it does change when I change the name or the email, but it doesn't when I update the image_path

MohamedTammam's avatar

@t9dev Did you read the file system part in the docs? the link that I posted? If not, please do. It describe how you can upload files.

1 like
MohamedTammam's avatar

@t9dev As I mentioned earlier, you use that line to store the image and get the new path.

$image_path = $request->file('image')->store('images')

Before that you need to run php artisan storage:link as mentioned in the docs.

One more thing, did you add enctype="multipart/form-data" to your form?

t9dev's avatar
Level 2

@MohamedTammam , yes it is added. I tried the Storage way, but where does it store the image? It doesn't apper.

t9dev's avatar
Level 2

@MohamedTammam , Is this the right way?

company::insert([
            'name' => $request->name,
            'eco_sector' => $request->eco_sector,
            'sector' => $request->sector,
            'email' => $request->email,
            'phone_number' => $request->phone_number,
            'image_path' => $request->file('image')->store('images'),
            'created_at' => Carbon::now()

        ]);
t9dev's avatar
Level 2

@MohamedTammam , how do I create the file inside the public file, because this line creates the file in the app directory.

$request->file('image')->store('images'),
t9dev's avatar
Level 2

@MohamedTammam , After all of this I am still having the same problem, the image is deleted but the new image_path in not updated in the DB.

if ($request->hasFile('image')) {
            $Image = company::find($id);
            $oldImageName = $Image->image_path;
            Storage::disk('public')->delete($oldImageName);

            company::find($id)->update([
                'name' => $request->name,
                'eco_sector' => $request->eco_sector,
                'sector' => $request->sector,
                'email' => $request->email,
                'phone_number' => $request->phone_number,
                'image_path' => $request->file('image')->store('images', 'public'),
                'updated_at' => Carbon::now(),
            ]);
t9dev's avatar
Level 2

@adnanerlansyah403 , okay.

<form class="row g-3 pt-3" action="{{route('company.update',$companyData->id)}}" method="POST" enctype="multipart/form-data">
                    @csrf
                    @method('PUT')
                    <div class="col-md-6">
                        <label for="inputEmail4" class="form-label">Company Name</label>
                        <input name="name" type="text" class="form-control" value="{{$companyData->name}}">

                        @error('name')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    <div class="col-3">
                        <label for="inputAddress" class="form-label">Economic Sector</label>
                        <input name="eco_sector" type="text" class="form-control" value="{{$companyData->eco_sector}}">
                        @error('eco_sector')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    <div class="col-3">
                        <label for="inputSector" class="form-label">Sector</label>
                        <input  name="sector" type="text" class="form-control" id="inputAddress" value="{{$companyData->sector}}">
                        @error('sector')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Email</label>
                        <input name="email" type="email" class="form-control" id="svemail" value="{{$companyData->email}}">
                        @error('email')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    <div class="col-6">
                        <label for="inputAddress" class="form-label">Office Phone Number</label>
                        <input  name="phone_number" type="text" class="form-control" id="inputAddress" value="{{$companyData->phone_number}}">
                        @error('phone_number')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    <div class=" col-6 input-group ">
                        <label for="inputAddress" class="form-label">Company Image</label>
                        <div class="input-group mb-3">
                            <input name="image" type="file" class="form-control" id="inputGroupFile02" value="{{$companyData->image_path}}">
                            <label  class="input-group-text" for="inputGroupFile02">Upload</label>
                        </div>
                        <figure class="figure">
                            <img class="figure-img img-fluid rounded" src="{{Storage::URL($companyData->image_path)}}" width="180" alt="Company Image">
                            <figcaption class="figure-caption text-left text-white">Current Image</figcaption>
                        </figure>
                        
                        @error('image')
                            <span class="text-light">{{'*'.$message}}</span>
                        @enderror
                    </div>
                    
                    <div class="col-3" style="margin-bottom: 1rem">
                        <button type="submit" class="btn btn-success"><i class="fa fa-plus" aria-hidden="true"></i> Update Record</button>
                        <a href="{{URL::to('company')}}"><button class="btn btn-danger float-right"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i> Go Back</button></a>
                    </div>
                </form>
adnanerlansyah403's avatar

@t9dev why in input company image have a value image_path. I think it shouldn't be set the value. So you just set for the name's of input.

Snapey's avatar
Snapey
Best Answer
Level 122

is image_path fillable in your model?

1 like
t9dev's avatar
Level 2

@Snapey , yes it is.

protected $fillable = [
        'name',
        'eco_sector',
        'sector',
        'email',
        'phone_number',
        'image_path',
    ];
t9dev's avatar
Level 2

My mistake was that in the model it is stated as image instead of image_path, thank you everyone. And thanks to @mohamedtammam for providing me with a new way of storing files.

huyvuong's avatar

You can refactor if-else condition and put return outside the if-else because they are the same. Try to make your code clearly as much as you can.

1 like
huyvuong's avatar

@t9dev

$updateData = [
'name' => $request->name,
'eco_sector' => $request->eco_sector,
'sector' => $request->sector,
'email' => $request->email,
'phone_number' => $request->phone_number,
'updated_at' => Carbon::now(),
];

if ($request->file('image')) {
	$image_path = $this->imageRequestHandleFunc($request->file('image'));
	$updateData['image_path'] = $image_path;
}
$updateResult = $this->updateInfo($updateData);
if ($updateResult) {
	// return redirect with success msg
} else {
	// return redirect with fail msg
}

function imageRequestHandleFunc($file) {
	// some code
	return $image_path;
}

function updateInfo($updateData) {
	// update code
	// return true or false;
}

this is not that good but I think it's easy to read and prevent duplicate

1 like

Please or to participate in this conversation.