更新Laravel 9时数据库中的数据未更新

fkvaft9z  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(323)

我尝试过更新类别信息,但数据库中没有更新,我尝试返回$category;**$类别-〉更新();**并看到它的更新。但在数据库中看不到更新的数据。

1.网页

// Admin Dashboard Route

路由::前缀('admin')-〉中间件(['auth','isAdmin'])-〉组(函数(){

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.控制器

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.检视

@extends('layouts.admin')

@区段('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>

@结束部分

ax6ht2ek

ax6ht2ek1#

update更改为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();
5n0oy7gb

5n0oy7gb2#

您的代码中有两个错误步骤:
从数据库中提取类别记录后,您正在创建新副本,您应该删除此步骤。然后您应该使用保存()方法,而不是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;
f4t66c6m

f4t66c6m3#

您可以使用Route Model Binding稍微缩短代码,然后使用上面提到的save()方法。

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');
}

如果您真的想使用update()方法,您也可以:

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');
}

相关问题