TheBlueDragon's avatar

How to change the request input ?

hello every one

i have a Category model and its have only name and image field

so for making a new category its work fine

but when its come to update

i have a small issue

i make the code work like this :-

1 - if the user upload a file then it will take the file and put the path on the field 2 - if not then it will grab the previous value of the image field and put it in the field

the problem with the first step

its upload the file but its return an empty value in the image field and dosnt update it !!

this is my code

public function update($id, Request $request) { $category = Category::FindOrFail($id);

  // check if the request has a file
  if($request->hasFile('image')) // if yes it will upload it 
  {
        
      // generate the file with random name and its original extension
      $extension = $request->file('image')->getClientOriginalExtension();
      $filename = str_random(5);
      $image = $filename .'.'. $extension;
      // after uploading to disk it will be moved to another directory
      // and deleted from the storage directory 
      Storage::disk('local')->put($image,File::get($request->file('image')));
      $request->file('image')->move(public_path().'/images/categories/',$image);
      Storage::delete($image);
      // change the request image to the new path and update the category
      $request['image'] = public_path().'/images/categories/'.$image;
      $category->update($request->all());
      
      return $category;

  }else // if not it will pass the previous path to the request and save it
  {
      $request['image'] = $category->image;
      $category->update($request->all());
      
      return $category;
  }

}

0 likes
8 replies
bobbybouwmann's avatar

You can do this, you get the idea ;)

public function store(Request $request)
{
    $request->merge([
        'field' => $value,  
        'field2' => $value2
    ]);

    Category::update($request->all());
}
TheBlueDragon's avatar

thanks @bobbybouwmann for reply

hmm i try ur code but

its give same result

{ "id": 1, "name": "Small Car2", "created_at": "2015-05-21 12:51:11", "updated_at": "2015-05-22 08:14:11", "image": {} }

and when i check the field in the DB it have this value C:\wamp\tmp\phpE1D0.tmp

and in the file was already move to the new directory

also if u see my code u will see that iam using same thing to change the request in the else statement and its work fine !!

bobbybouwmann's avatar

The problem is not the request, it's how you save your image! Your question was how to change the input of the request and I gave you that answer!

If you want the file name and the correct location you can do something like this

$file = Input::file('image');

$file = $file->move(public_path() . '/images/'. time() . '-' . $file->getClientOriginalName());

$request->merge([
    'image' => $file->getRealPath()
]);

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

Note: you can use this as a reference: https://laracasts.com/lessons/file-uploads-101

TheBlueDragon's avatar

@bobbybouwmann ur code give same result of my code >_<

i already use same as yours before and have this result and i ready the docs and use the code before i was think it was change from L4 but also same result

the result give

{ "id": 1, "name": "Small Car2", "created_at": "2015-05-21 12:51:11", "updated_at": "2015-05-22 08:40:37", "image": {} }

empty image !!! and in the DB field it give C:\wamp\tmp\php170C.tmp >_<

bashy's avatar

So you tested what $file->getRealPath() returns? The temp path is the path before it's moved so you may have to do that differently.

You need to create the file name for the image and then move it with that image name

$file_name = time() . '-' . $file->getClientOriginalName();

You shouldn't use full paths for images incase you need to move them

$request->merge([
    'image' => $file_name,
]);
TheBlueDragon's avatar

but the thing whatever i write here
$request->merge([ 'image' => $file, ]);

or

$request['image'] = $file;

in both even hard coding a string in this dosnt change any thing !!!!!!!!

as for uploading and moving the file are working fine in store but in update its work but i cant change the request or save it or write any thing on it its still return an empty =/

TheBlueDragon's avatar
TheBlueDragon
OP
Best Answer
Level 15

no need i just find a way

$category->update([ 'name'=> $request->get('name'), 'image' => $request->get('image') ]);

will solve it

i dono why

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

have an issue >_<

Please or to participate in this conversation.