ziben69
1 month ago
195
3
Laravel

Store image in DB

Posted 1 month ago by ziben69

Hello guys, I have two tables in relation: Events and Photos. When adding a new event with a photo, event_id should appear in the Photos table. Unfortunately, the photo is added to the table, but event_id is empty.

Part of function store:

$event = new Event;
$event->name = $request->name;
$event->description = $request->description;

        if ($request->has('photos')){
            foreach ($request->photos as $photo) {  
                $filename = 'img-'.time().'.'.$photo->getClientOriginalExtension();
                $photo->storeAs('public/photos/',$filename);
                Photo::create([
                    'event_id' => $event->id,
                    'filename' => $filename
                ]);
            }
        }

The relationship is correct, and so are the models. There is some mistake in this passage. How can I improve the above fragment so that with adding a photo to the database, event_id was sent?

Thanks

Please sign in or create an account to participate in this conversation.