dimanche 21 octobre 2018

How to add extra images upload buttons and save values in the table in Laravel 5.6?

I am working with Laravel 5.6 and mysql database. in my application I have vehicle data saving table and images saving table as uploads. relationship between Vehicle model and Upload model is like this, Vehicle Model

public function uploads()
    {
        return $this->hasMany(Upload::class);
    }

Upload Model

public function vehicle()
    {
        return $this->belongsTo(Vehicle::class);
    }

and uploads table structure is like this

id  filename  resized_name  original_name  vehicle_id
1   ddeerr     dfrerrerr     dfggdfgdfgdf      2
2   hyujjh     hyujkiolp     frtghyujnbff      5
3   frtghy     dfdfgdfgdf    dgddhdherere      6
4   dfgdfgd    sdsdgsdsds    sddgsgsgssgsg     6
5   xsdfgsd    hgfydiurio    jdkjfkjhkjfjdj    6

and I have vehicle table data edit link like this,

" >Edit</a>

when click some vehicle edit link the url is like this,

http://localhost:8000/myads/11/edit

and in vehicle edit form related images showing with following codes,

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

                    <img id="preview"
                         src=""
                         height="200px" width="200px"/>
                    <input class="form-control" style="display:none" name="files[]" type="file" id="files" name="_token" value="" enctype="multipart/form-data">
                    <br/>

               <a href="/myads//editimage">Edit Image</a>|

               <a class="button is-outlined" href="/myads//delete" onclick="return confirm('Are you sure to want to delete this record?')" >Delete</a></td>

                  <hr>
                   @endforeach
                    @endif

now my problem is coming, actually in my application user can only upload images related each vehicle only 4 images. in data submition form user has facility to attach up to 1-4 images. then assuming one user who has attached only two images when he insert new vehicle data record. but after he needed attach more 2 images with his vehicle advertisment. then he should go edit form and he can seen existing his attach two images file and there should be two attach buttons to attach new two images. how can do this, I think we should check uploads table with related vehicle_id and if there are 4 images then he cannot attach onother images and if there are images less then 4 user can attach images up to 4 images in upload table.

  1. my new images store function is this,

    $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();
            } 
    
    

then in Image Controller how can I use above function to insert new attach images?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire