0

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

9
  • I believe this could be useful for you Commented Mar 14, 2021 at 1:19
  • I have a different situation. Thanks for help! :) Commented Mar 14, 2021 at 2:07
  • can you create public git repository and share?? Commented Mar 14, 2021 at 2:27
  • This project is my first order. I don’t think the client would like it if I expose it publicly. Tell me which part of the code you need, I'll show you. Commented Mar 14, 2021 at 2:59
  • it might help to see your database structure and see why some of those need to be duplicated Commented Mar 14, 2021 at 3:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Your example images are added once. my goal each room in the loop has its own images and adds them .
Thank you, very help me! :)
0

So your issue looks like you are overriding $file when creating your Image model.

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
    // Updated variable name
    $image = new Image();
    $image->src = $file_name;
    // save Model
    $image->save();
    $obj->file()->save($file);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.