Reputation: 79
I am following a tutorial and everything is working. The only problem is when I update category ID 1 and submit, I have now Category ID 2 with the updated data and Category ID 1 is still the same. Not sure what to do. I really need to continue but not sure how to fix this.
public function addEditCategory(Request $request, $id = NULL)
{
if(empty($id)) {
$title = "Add Category";
$category = new Category;
$categorydata = array();
$getCategories = array();
$message = "Category added successfully!";
} else {
$title = "Edit Category";
$categorydata = Category::where('id', $id)->first();
$getCategories = Category::with('subcategories')
->where([
'parent_id' => 0,
'section_id' => $categorydata['section_id']
])
->get();
$category = Category::find($id);
$message = "Category updated successfully!";
}
if($request->isMethod('post')) {
$data = $request->all();
$rules = [
'category_name' => 'required|regex:/^[\pL\s\-]+$/u',
'section_id' => 'required',
'url' => 'required',
'category_image' => 'image',
];
$customMessages = [
'category_name.required' =>'Category Name is required',
'category_name.regex' => 'Valid Category Name is required',
'section_id.required' => 'Section is required',
'url.required' => 'Category URL is required',
'category_image.image' => 'Valid Category Image is required',
];
// $this->validate($request, $rules, $customMessages);
if($request->hasFile('category_image')) {
$image_tmp = $request->file('category_image');
if($image_tmp->isValid()) {
$extension = $image_tmp->getClientOriginalExtension();
$imageName = rand(111,99999).'.'.$extension;
$imagePath = public_path('/uploads/admin/images/category_images/' . $imageName);
Image::make($image_tmp)->save($imagePath);
$category->category_image = $imageName;
}
}
if(empty($data['category_discount'])) {
$data['category_discount'] = blank('');
}
if(empty($data['description'])) {
$data['description'] = "";
}
if(empty($data['meta_title'])) {
$data['meta_title'] = "";
}
if(empty($data['meta_description'])) {
$data['meta_description'] = "";
}
if(empty($data['meta_keywords'])) {
$data['meta_keywords'] = "";
}
if(empty($data['url'])) {
$data['url'] = "";
}
$category->parent_id = $data['parent_id'];
$category->section_id = $data['section_id'];
$category->category_name = $data['category_name'];
$category->category_discount = $data['category_discount'];
$category->description = $data['description'];
$category->url = $data['url'];
$category->meta_title = $data['meta_title'];
$category->meta_description = $data['meta_description'];
$category->meta_keywords = $data['meta_keywords'];
$category->status = 1;
$category->save();
Session::flash('success', $message);
return redirect('admin/categories');
}
$sections = Section::get();
return view('admin.categories.add_edit_category')->with(compact('title', 'sections', 'categorydata', 'getCategories'));
}
Upvotes: 1
Views: 711
Reputation: 25
1.You have to Debug and check are you sending id everytime empty or not. might be update category also empty id is passing then you have to send id while updating. 2.Once you pass id while updating dd($id) and check its format and data type resolve it and update if condition it will fix your problem.
Upvotes: 0
Reputation: 79
That code is a disaster, you have the index, create, update method in the same controller, what course are you seeing? I recommend you see another.
Upvotes: 1