Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

elbojoloco's avatar

Issue with uploading and storing images url's in database

Hello,

I'm trying to find a good and easy way to store and update my images in the storage (s3 or public) and the according model's DB table. For example:

I have a Game model that is able to have 3 types of images

  • Logo
  • Banner
  • Poster

Now when I try to update that model and it's images, I don't seem to be able to do it properly.

This is the piece of code in my GameController@update I'm trying to do it with:

if ($request->logo && $file = Storage::putFileAs('images/games/logos', $request->file('logo'), $game->id)) {
    $request->request->add(['logo' => $file]);
}

if ($request->banner && $file = Storage::putFileAs('images/games/banners', $request->file('banner'), $game->id)) {
    $request->request->add(['banner' => $file]);
}

if ($request->poster && $file = Storage::putFileAs('images/games/posters', $request->file('poster'), $game->id)) {
    $request->request->add(['poster' => $file]);
}

$request->request->add(['slug' => str_slug($request->name)]);

$game->update($request->all());

however when i return $request->all() before updating, it shows logo, banner and poster as "{}", even when I upload them with my form. When I return a single image for example return $file, I see the correct pathname 'images/games/logos/7' and it is storing the image correctly aswell.

So my question is is there a better way to do this or am I doing something wrong, because right now my images aren't being stored in the model's database table.

0 likes
4 replies
Jaytee's avatar

Now i'm not familiar with the Request API so i can't confirm what $request->request->add([]); explicitly does but, make sure your form is accepting the enctype

<form method="POST" action="something" enctype="multipart/form-data">
elbojoloco's avatar

Yes like I said, the images are uploaded correctly. But I can't add the file names to the request so I can pass them to the $game->update() function. And $request->request->add() does work, because I use it for adding the slug and the slug is added successfully.

Jaytee's avatar
Jaytee
Best Answer
Level 39

Why not just assign $request to another variable such as $data?

$data = $request->all();

$data['slug'] = str_slug('some-value');

$game->update($data);

EDIT:

Righteo, I checked out the API for Request. You can do:

// request() is the same as $request. It's just a helper
request()->merge([
    'key' => 'value
]);
elbojoloco's avatar

I found the issue, apperantly you're not allowed to overwrite values that are already in the request object. So I prefixed my form image inputs with an f and then created a $data array that stores the new images and slug and afterwards merges them to the request with the correct key.

$data['logo'] = $request->flogo ? $request->flogo->storeAs('images/games/logos', $game->id) : $game->logo;
$data['banner'] = $request->fbanner ? $request->fbanner->storeAs('images/games/banners', $game->id) : $game->banner;
$data['poster'] = $request->fposter ? $request->fposter->storeAs('images/games/posters', $game->id) : $game->poster;
$data['slug'] = str_slug($request->name);

$request->merge($data);

$game->update($request->all());

This is shorter and works perfect.

Please or to participate in this conversation.