You can do this, you get the idea ;)
public function store(Request $request)
{
$request->merge([
'field' => $value,
'field2' => $value2
]);
Category::update($request->all());
}
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;
}
}
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.