Msoft's avatar
Level 1

Why did not Update Images in edit view file in Laravel 5.6?

I have edit blade file in my laravel application. in this edit form update data saving to two tables to vehicles and uploads. I have following codes in edit blade file update images to uploads table,

@foreach( $vehicles-> uploads as $upload)

                    <img id="preview"
                         src="{{asset((isset($upload) && $upload->resized_name!='')?'images/'.$upload->resized_name:'images/noimage.png')}}"
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
                    <br/>
                    <a href="javascript:changeProfile();">Add Image</a> |
                  <a class="button is-outlined" href="/myads/{{$upload->id}}/delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>
<hr>
                   @endforeach
                    @endif 

update controller is,

$photos = $request->file('files');

        if (!is_array($photos)) {
            $photos = [$photos];
        }

        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }

        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();

            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);

            $photo->move($this->photos_path, $save_name);

            $upload = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        } 

and route is,

Route::post('myads/{id}', [
    'uses' => '\App\Http\Controllers\VehicleController@update',
])->name('vehicles.edit');

but when I attach image and click update buttons it is not update image in uploads table. how can fix this problem?

0 likes
8 replies
Cronix's avatar

Where does $id come from here?

$upload = Upload::find($id);

Does the image get saved on the filesystem and just not in the db?

Why do you have enctype="multipart/form-data" on the input? It should be on the <form> open tag.

<input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
Msoft's avatar
Level 1

@Cronix see this is my full form.

<form  method="post"  action="{{ route('vehicles.edit', $vehicles->id)  }}" enctype="multipart/form-data">
                {{csrf_field()}}
                <div class="form-group{{ $errors->has('provincename') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">Province</label>
        <select name="provincename" id="provincename" class="form-control input dynamic" data-dependent="districtname" >
            <option value="{{$vehicles->provincename}}">{!! $vehicles->provincename !!}</option>
            @foreach($town_list as $town)
            
            <option value="{{$town->provincename}}">{{$town->provincename}}</option>
            @endforeach
        </select>
         @if ($errors->has('provincename'))
                    <span class="help-block">{{ $errors->first('provincename') }}</span>
                @endif
        </div>

        <div class="form-group{{ $errors->has('districtname') ? ' has-error' : '' }}">
            <label for="exampleFormControlSelect1">District</label>
           <select name="districtname" id="districtname" class="form-control input dynamic" data-dependent="townname" >
            <option value="{{$vehicles->districtname}}">{!! $vehicles->districtname !!}</option>

            
            
        </select>
         @if ($errors->has('districtname'))
                    <span class="help-block">{{ $errors->first('districtname') }}</span>
                @endif
        </div>

<input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="{{ csrf_token() }}" enctype="multipart/form-data">
<button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
        </div>
</form>

and this is the full update controller,

public function update(Request $request, $id)
    {
 $vehicle = Vehicle::find($id);

        $vehicle->provincename = $request->input('provincename');
        $vehicle->districtname = $request->input('districtname');
 $photos = $request->file('files');
 
        if (!is_array($photos)) {
            $photos = [$photos];
        }
 
        if (!is_dir($this->photos_path)) {
            mkdir($this->photos_path, 0777);
        }
 
        for ($i = 0; $i < count($photos); $i++) {
            $photo = $photos[$i];
            $name = sha1(date('YmdHis') . str_random(30));
            $save_name = $name . '.' . $photo->getClientOriginalExtension();
            $resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
 
            Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);
 
            $photo->move($this->photos_path, $save_name);
 
            $upload = Upload::find($id);
            $upload->filename = $save_name;
            $upload->resized_name = $resize_name;
            $upload->original_name = basename($photo->getClientOriginalName());
            $upload->vehicle_id = $vehicle->id;
            $upload->save();
        }

Cronix's avatar

And did the image save on the filesystem? In the form, why are you giving the file input the value of the csrf token? Are there any errors? Is there anything in your laravel log file about the problem?

Msoft's avatar
Level 1

@Cronix not any error image save in the uploads table, it structure is like this,

id  imagename  vehicle_id
1       edgrt                   5
2      hgyti                    7
3      hgytu                  9
4      hyjjjjii                  6

then when I go to edit vehicle id number 5, like this

http://localhost:8000/myads/5/edit

route,

Route::get('myads/{id}/edit', [
    'uses' => '\App\Http\Controllers\VehicleController@edit',
    'as'=> 'vehicles.edit'
]);

I need update relevant vehicle_id number on the update table

Cronix's avatar

Please answer each question. I ask them bc I need to know more in order to help you, and you're doing some strange things.

I didn't ask if the image saved in the database. You said up front it's not. I'm asking if it saved on the filesystem.

Msoft's avatar
Level 1

@Cronix I cant understand what did you mean by ** I'm asking if it saved on the filesystem.**

Cronix's avatar

You're saving the file to the disk here

Image::make($photo)
                ->resize(250, null, function ($constraints) {
                    $constraints->aspectRatio();
                })
                ->save($this->photos_path . '/' . $resize_name);
 
            $photo->move($this->photos_path, $save_name);

is that working?

Msoft's avatar
Level 1

@Cronix My image store function is same to here it is working. I mean images are saving to Uploads table.

Please or to participate in this conversation.