I have tried to update category information and it's updated but not updated in the database. I am trying to return $category; before $category->update(); and see it's updated. But in the database not see updated data.
1.Web
// Admin Dashboard Route
Route::prefix('admin')->middleware(['auth','isAdmin'])->group(function () {
Route::get('dashboard', [App\Http\Controllers\Admin\DashboardController::class, 'index']);
// Category Route
Route::controller(App\Http\Controllers\Admin\CategoryController::class)->group(function () {
Route::get('/category', 'index');
Route::get('/category/create', 'create');
Route::post('/category', 'store');
Route::get('/category/view/{id}', 'view');
Route::get('/category/{category}/edit', 'edit');
Route::put('/category/{category}', 'update');
});
});
2. Controller
public function update(CategoryFormRequest $request, $category){
$category = Category::findOrFail($category);
$validatedData = $request->validated();
$category = new Category;
$category->name = $validatedData['name'];
$category->slug = Str::slug($validatedData['slug']);
$category->description = $validatedData['description'];
if($request->hasFile('image')){
$path = 'uploads/category/' .$category->image;
if(File::exists($path)){
File::delete($path);
}
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$fileName = 'PC' .'-'. time() .'.'. $ext;
$file->move('uploads/category/', $fileName);
$category->image = $fileName;
}
$category->meta_title = $validatedData['meta_title'];
$category->meta_keywords = $validatedData['meta_keywords'];
$category->meta_description = $validatedData['meta_description'];
$category->status = $request->status == true ? '0':'1';
$category->update();
return $category;
return redirect('admin/category')->with('message','Category Updated Successfully');
}
3. View
@extends('layouts.admin')
@section('content')
<div >
<div >
<div >
<div >
<h4 >Edit Category</h4>
<a href="{{ url('admin/category') }}" >View Category</a>
</div>
<form action="{{ url('admin/category/'.$category->id) }}" method="POST" enctype="multipart/form-data">
@method('PUT')
@csrf
<div >
<div >
<div >
<label for="" >Status</label>
<input type="checkbox" name="status" {{ $category->status == 0 ? 'Checked':'' }}>
</div>
<div >
<label for="" >Name</label>
<input type="text" name="name" id="" value="{{ $category->name }}" placeholder="Enter category name">
@error('name')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<label for="" >Slug</label>
<input type="text" name="slug" id="" value="{{ $category->slug }}" placeholder="Enter category slug">
@error('slug')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<label for="" >Image</label>
<input type="file" name="image" id="">
<img src="{{ asset('uploads/category/' .$category->image) }}" width="60" height="60" alt="">
@error('image')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<label for="" >Description</label>
<textarea name="description" id="" rows="5" placeholder="Enter Description">{{ $category->description }}</textarea>
@error('description')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<h4>SEO Tags</h4>
</div>
<div >
<label for="" >Meta Title</label>
<input type="text" name="meta_title" value="{{ $category->meta_title }}" id="" placeholder="Enter meta title">
@error('meta_title')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<label for="" >Meta Keywords</label>
<textarea name="meta_keywords" id="" rows="3" placeholder="Enter Meta keywords">{{ $category->meta_keywords }}</textarea>
@error('meta_keywords')
<small >{{ $message }}</small>
@enderror
</div>
<div >
<label for="" >Meta Description</label>
<textarea name="meta_description" id="" rows="3" placeholder="Enter Meta Description">{{ $category->meta_description }}</textarea>
@error('meta_description')
<small >{{ $message }}</small>
@enderror
</div>
</div>
</div>
<div >
<div >
<button type="submit" >Update Category</button>
</div>
</div>
</form>
</div>
</div>
</div>
@endsection
CodePudding user response:
Change update
with save
$category->meta_title = $validatedData['meta_title'];
$category->meta_keywords = $validatedData['meta_keywords'];
$category->meta_description = $validatedData['meta_description'];
$category->status = $request->status == true ? '0':'1';
$category->save();
CodePudding user response:
you have two wrong steps in your code:
after fetching the category record from the db, you are creating new copy, you should remove this step. then you should use save() method, not update.
$category = Category::findOrFail($category);
$validatedData = $request->validated();
// $category = new Category; remove this line
$category->name = $validatedData['name'];
$category->slug = Str::slug($validatedData['slug']);
$category->description = $validatedData['description'];
if($request->hasFile('image')){
$path = 'uploads/category/' .$category->image;
if(File::exists($path)){
File::delete($path);
}
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$fileName = 'PC' .'-'. time() .'.'. $ext;
$file->move('uploads/category/', $fileName);
$category->image = $fileName;
}
$category->meta_title = $validatedData['meta_title'];
$category->meta_keywords = $validatedData['meta_keywords'];
$category->meta_description = $validatedData['meta_description'];
$category->status = $request->status == true ? '0':'1';
$category->save(); // save not update
return $category;