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

hjortur17's avatar

Update a image path

Hello I'm building a dashboard with a update feature. On my thread I have image witch I store like this:

$camping = Camping::create([
                     'title'         =>      request('title'),     
                     'en_title'      =>      request('en_title'),  
                     'state'         =>      request('states'),     
                     'address'       =>      request('address'),   
                     'body'          =>      request('body'),      
                     'en_body'       =>      request('en_body'),   
                     'phone'         =>      request('phone'),     
                     'email'         =>      request('email'),     
                     'website'       =>      request('website'),   
                     'opening'       =>      request('opening'),
                     'image_path'    =>      request()->file('image')->store('images', 'public')
 ]);

And now I'm trying to figure out how to update it when I post the update form. This is how the controller looks like right now:

public function update(Request $request, $id)
       {
              $camping = Camping::where('id', '=', $id)->first();
              $camping->update($request->all());
              $request->image->store('images', 'public');

              return redirect()->route('dashboard');
       }
0 likes
5 replies
Cronix's avatar
Cronix
Best Answer
Level 67

You'd need to handle setting the image path in update() just like you are in the create method. You're not just dumping $request to create. You're manipulating the data before you send it to create. You need to do the same in edit, or at least for the image_path variable.

// make an array of all request data except for the image path
$data = $request->except('image_path');

// now set the image path just like in create.
$data['image_path'] = request()->file('image')->store('images', 'public');

// update
$camping->update($data);

I'd actually redo your create() method too. Notice how much simpler it is to do

$data = $request->except('image_path');
$data['image_path'] = request()->file('image')->store('images', 'public');
$camping = Camping::create($data);

vs your

$camping = Camping::create([
                     'title'         =>      request('title'),     
                     'en_title'      =>      request('en_title'),  
                     'state'         =>      request('states'),     
                     'address'       =>      request('address'),   
                     'body'          =>      request('body'),      
                     'en_body'       =>      request('en_body'),   
                     'phone'         =>      request('phone'),     
                     'email'         =>      request('email'),     
                     'website'       =>      request('website'),   
                     'opening'       =>      request('opening'),
                     'image_path'    =>      request()->file('image')->store('images', 'public')
 ]);

There is more info about what you can do with the request object in the user guide if you care to learn more.

1 like
hjortur17's avatar

@cronix - Now I'm getting this error on the update function (The store function worked).

Call to a member function store() on null

This is how the function looks like:

public function update(Request $request, $id)
       {
              // $data = Camping::where('id', '=', $id)->first();
              $data = $request->except('image_path');
              $data['image_path'] = request()->file('image')->store('images', 'public');
              $camping->update($data);

              return redirect('/stjornbord/tjaldsvæði/breyta');
       }

And the input:

<span>Veldu mynd</span>
<input type="file" name="image" class="hidden">
Cronix's avatar

The way you wrote the original code, it expects an image. I take it you didn't select one? If the image isn't required, then you need to check to see if the image exists in the request before trying to save it. Validation rules would also prevent the error and let the user know.

If no image is selected, this becomes null

request()->file('image')

so it ends up null->store('images', 'public') which obviously is a problem.

hjortur17's avatar

@cronix - I am passing in an image but it always returns with Call to a member function store() on null. I tried dd and it showed like an image was in the request.

Please or to participate in this conversation.