skip to Main Content

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.

enter image description here

1.Web

// Admin Dashboard Route

Route::prefix(‘admin’)->middleware([‘auth’,’isAdmin’])->group(function () {

Route::get('dashboard', [AppHttpControllersAdminDashboardController::class, 'index']);

// Category Route
Route::controller(AppHttpControllersAdminCategoryController::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 class="row">
    <div class="col-md-12">
        <div class="card">
            <div class="card-header d-flex align-items-center justify-content-between">
                <h4 class="mb-0">Edit Category</h4>
                <a href="{{ url('admin/category') }}" class="btn btn-primary btn-sm float-end text-light">View Category</a>
            </div>
            <form action="{{ url('admin/category/'.$category->id) }}" method="POST" enctype="multipart/form-data">
                @method('PUT')
                @csrf
                
                <div class="card-body">
                
                    <div class="row">

                        <div class="col-md-12 mb-3">
                            <label for="" class="form-label">Status</label>
                            <input type="checkbox" name="status" {{ $category->status == 0 ? 'Checked':'' }}>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="" class="form-label">Name</label>
                            <input type="text" class="form-control" name="name" id="" value="{{ $category->name }}" placeholder="Enter category name">
                            @error('name')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="" class="form-label">Slug</label>
                            <input type="text" class="form-control" name="slug" id="" value="{{ $category->slug }}" placeholder="Enter category slug">
                            @error('slug')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="col-md-6 mb-3">
                            <label for="" class="form-label">Image</label>
                            <input type="file" class="form-control" name="image" id="">
                            <img src="{{ asset('uploads/category/' .$category->image) }}" width="60" height="60" class="img-fluid rounded-top" alt="">
                            @error('image')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>
                        <div class="col-md-6 mb-3">
                            <label for="" class="form-label">Description</label>
                            <textarea class="form-control" name="description" id="" rows="5" placeholder="Enter Description">{{ $category->description }}</textarea>
                            @error('description')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="col-md-12 mb-3">
                            <h4>SEO Tags</h4>
                        </div>

                        <div class="col-md-12 mb-3">
                            <label for="" class="form-label">Meta Title</label>
                            <input type="text" class="form-control" name="meta_title" value="{{ $category->meta_title }}" id="" placeholder="Enter meta title">
                            @error('meta_title')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="col-md-12 mb-3">
                            <label for="" class="form-label">Meta Keywords</label>
                            <textarea class="form-control" name="meta_keywords" id="" rows="3" placeholder="Enter Meta keywords">{{ $category->meta_keywords }}</textarea>
                            @error('meta_keywords')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                        <div class="col-md-12 mb-3">
                            <label for="" class="form-label">Meta Description</label>
                            <textarea class="form-control" name="meta_description" id="" rows="3" placeholder="Enter Meta Description">{{ $category->meta_description }}</textarea>
                            @error('meta_description')
                                <small class="text-danger">{{ $message }}</small>
                            @enderror
                        </div>

                    </div>
                    
                </div>
                <div class="card-footer">
                    <div class="col-12 text-center">
                        <button type="submit" class="btn btn-primary text-light">Update Category</button>
                    </div>
                </div>
            </form>    
        </div>
    </div>
</div>

@endsection

4

Answers


  1. 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();
    
    Login or Signup to reply.
  2. 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;
    
    Login or Signup to reply.
  3. You could shorten up the code a little using Route Model Binding, then use the save() method as mentioned above.

    public function update(CategoryFormRequest $request, Category $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;
    
        return redirect('admin/category')->with('message','Category Updated Successfully');
    }
    

    If you really want to use the update() method you can as well:

    public function update(CategoryFormRequest $request, Category $category){
    
        $validated = $request->validated();
        $filename;
    
        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->update([
            'name' => $$validated['name'],
            'slug' => Str::slug($$validated['slug']),
            'description' => $$validated['description'],
            'meta_title' => $$validated['meta_title'],
            'meta_keywords' => $$validated['meta_keywords'],
            'meta_description' => $$validated['meta_description'],
            'status' => $validated['status'] == true ? '0' : '1',
            'image' => $filename,
        ]);
    
        return $category;
    
        return redirect('admin/category')->with('message','Category Updated Successfully');
    }
    
    Login or Signup to reply.
  4. remove the line:

     $category = new Category;
    

    use save instead of update:

    $category->save();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search