Azik's avatar
Level 17

Image upload to pivot table

I have cars , images and car_image tables in addController.php I get an array of images name and save to storage floder and database too. store function

  public function store(Request $request)
  {   
      $array = [];
    $this->validate($request, [
      'profile_image.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
    ]);
    $car = Car::create([
          'user_id' => $request->user_id,
        ]);   
    if ($request->hasFile('profile_image')) {
    foreach ($this->uploadImage($request) as $image) {
      $array[] = Photo::create(['image' => $image])->id;
      }
      $car->images()->attach($array);
    }
    
    return redirect()->back()->with('success', "image successfully uploaded!!");
  }

and uploadImage function

 protected function uploadImage($request){
    $imageall = [];
      foreach($request->file('profile_image') as $file){
        $filenamewithextension = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $filenametostore = 'getauto_'.uniqid().'.'.$extension;
        $imageall[] = $filenametostore;
        Storage::put('public/cars/'. $filenametostore, fopen($file, 'r+'));
        Storage::put('public/cars/thumb/'. $filenametostore, fopen($file, 'r+'));
        $thumbnailpath = public_path('storage/cars/thumb/'.$filenametostore);
        $mainimagepath = public_path('storage/cars/'.$filenametostore);
        }
        return $imageall;
     }

the problem is updating function how can i update and delete which related user images ??

0 likes
2 replies

Please or to participate in this conversation.