phplaravel

Laravel I try to duplicate an object and add pictures in each object


I have a room to add. And there are fields called pieces. If the user has written all the fields, he chose pictures for the room. and wrote 5 pieces. I want to add 5 pieces to the same room with selected fields and photos. That is, duplicate the same object in several pieces.

RoomController store function

    public function store(Request $request)
    {
        $request->validate([
            'title.*' => 'required',
            'content.*' => 'required',
            'people' => 'required',
            'hotel_id' => 'required',
            'night_price' => 'required',
            'pieces' => 'required',
            'single_bed' => 'required',
            'double_bed' => 'required',
            'roomImages' => 'required',
            'roomImages.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ]);

        $rooms = array_fill(1, $request->get('pieces'), $request->all());
        foreach ($rooms as $room){
            dd($rooms);
            $obj = new Room();
            // Set translations
            $obj->setTranslations('title', $room['title']);
            $obj->setTranslations('content', $room['content']);

            // Save object
            $obj->amenities = $room['amenities'];
            $obj->fill($room);
            $obj->save();

            foreach ($room['roomImages'] as $file) {
                $file_name = time() . '_' . md5($file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
                $file->move(public_path('/uploads/rooms/'), $file_name);
                // Save image in Model Image
                $file = new Image();
                $file->src = $file_name;
                $obj->file()->save($file);
            }
        }


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

Error enter image description here

One circle of the foreach is running, the object with the pictures is added. On the 2nd lap, the foreach stops and shows this error


Solution

  • Try the bellow code

    foreach ($request->roomImages as $file) {
        $file_name = time() . '_' . md5($file->getClientOriginalName()) . '.' . $file->getClientOriginalExtension();
        $file->move(public_path('/uploads/rooms/'), $file_name);
    }
    
    $rooms = array_fill(1, $request->get('pieces'), $request->all());
    foreach ($rooms as $key => $room){
        $obj = new Room();
        // Set translations
        $obj->setTranslations('title', $room['title']);
        $obj->setTranslations('content', $room['content']);
    
        // Save object
        $obj->amenities = $room['amenities'];
        $obj->fill($room);
        $obj->save();
    
    
        \File::copy(public_path('/uploads/rooms/'.$file_name), public_path('/uploads/rooms/'.$key.$file_name));
    
        $file = new Image();
        $file->src = $key.$file_name;
    
        $obj->file()->save($file);
    }
    \File::delete(public_path('/uploads/rooms/'.$file_name));
    

    The move function will delete the file from the original request. So when the loop run second time, you are getting error.