I'm only getting an error while trying to update the data. I'm using post method not the push. I've tried to add @method('PUT') but that didn't work, too.
Creating new category just works fine as it should do but when I try to edit something it just throws the error. Also I tried to use route::put but that didn't work.
My CategoryController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$data = Category::all();
return view('admin.category.index', [
'data' => $data
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.category.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = new Category;
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Display the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function edit(Category $category, $id)
{
//
$data = Category::find($id);
return view('admin.category.edit', [
'data' => $data
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category, $id)
{
//
$data = Category::find($id);
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
MY edit.blade.php
@extends('layouts.adminbase')
@section('title', 'Edit Category')
@section('content')
<!-- Begin Page Content -->
<div >
<!-- Page Heading -->
<h1 >Edit Category: {{$data -> title}}</h1>
<!-- Collapsable Card-->
<div >
<!-- Card Header - Accordion -->
<a href="#collapseCardExample" data-toggle="collapse"
role="button" aria-expanded="true" aria-controls="collapseCardExample">
<h6 >Category Elements</h6>
</a>
<!-- Card Content - Collapse -->
<div id="collapseCardExample">
<div >
<form action="/admin/category/update/{{$data->id}}" method="POST">
@csrf
<div >
<div >
<label for="exampleInputEmail1">Title</label>
<input type="text" name="title" value="{{$data->title}}">
</div>
<div >
<label for="exampleInputEmail1">Keywords</label>
<input type="text" name="keywords" value="{{$data->keywords}}">
</div>
<div >
<label for="exampleInputEmail1">Description</label>
<input type="text" name="description" value="{{$data->description}}">
</div>
<div >
<label for="exampleInputFile">Image</label>
<div >
<div >
<input type="file" name="image">
<label for="exampleInputFile">Choose image file</label>
</div>
<div >
<span >Upload</span>
</div>
</div>
</div>
<div >
<label>Status</label>
<select name="status">
<option selected>{{$data->status}}</option>
<option>Enable</option>
<option>Disable</option>
</select>
</div>
</div>
<!-- /.card-body -->
<div >
<button type="submit" >Update Data</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
@endsection
My routes
// Admin category routes
Route::get('/admin/category', [AdminCategoryController::class,'index'] )->name('admin_category');
Route::get('/admin/category/create', [AdminCategoryController::class,'create'] )->name('admin_category_create');
Route::post('/admin/category/store', [AdminCategoryController::class,'store'] )->name('admin_category_store');
Route::get('/admin/category/edit/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_edit');
Route::post('/admin/category/update/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_update');
CodePudding user response:
I'd make these modifications to your code: edit.blade:
<form action="/admin/category/update/{{$data->id}}" method="POST">
@csrf
@method('PUT')
.........
Route:
Route::put('/admin/category/update/{id}', [AdminCategoryController::class,'update'] )->name('admin_category_update');
Check if it works.
One more thing: If you correctly created model binding, your update function could look like this:
public function update(Request $request, Category $category, $id)
{
//
$category->parent_id = 1;
$category->title = $request->title;
$category->keywords = $request->keywords;
$category->description = $request->description;
$category->status = $request->status;
$category->save();
return redirect('admin/category');
}
That is because you already have the model right there: public function update(Request $request, **Category $category**, $id)