I have been having this error no matter what i do.
This is the web.php page. I tried to creat a form that would add a product, when I tried to a an item it would show me this. I am new to laravel and don’t have a clue about this
Route::group(['middleware' => 'admin.auth'],function(){
// Categories Routes
Route::get('/categories/create',[CategoryController::class,'create'])->name('categories.create');
Route::post('/categories',[CategoryController::class,'store'])->name('categories.store');
});
CategoryController.php
class CategoryController extends Controller
{
public function index() {
}
public function create() {
return view('admin.category.create');
}
public function store(Request $request) {
$validator =Validator::make($request->all(),[
'name' => 'required',
'slug' =>'required|unique:categories',
]);
if ($validator->passes()) {
$category = new Category();
$category->name = $request->name;
$category->slug = $request->slug;
$category->status = $request->status;
$category->save();
return response()->json([
'status' => true,
'message' => 'Category Added Successfully'
]);
$request->session()->flash('success','Category Added Successfully');
} else {
return response()->json([
'status' => false,
'errors' => $validator->error()
]);
}
}
}
ajax that is used inside the form blade to enter the name and slug of the product
$.ajax({
url: '{{ route("categories.store") }}',
type: 'post',
data: element.serializeArray(),
dataType: 'json',
success: function(response){
if (response["status"] == true) {
$("#name").removeClasss('is-invalid')
.siblings('p')
.removeClass('invalid-feedback').html("");
$("#slug").removeClasss('is-invalid')
.siblings('p')
.removeClass('invalid-feedback').html("");
} else {
var errors = response['errors'];
if (errors['name']) {
$("#name").addClasss('is-invalid')
.siblings('p')
.addClass('invalid-feedback').html(errors['name']);
} else {
$("#name").removeClasss('is-invalid')
.siblings('p')
.removeClass('invalid-feedback').html("");
}
if (errors['slug']) {
$("#slug").addClasss('is-invalid')
.siblings('p')
.addClass('invalid-feedback').html(errors['slug']);
} else {
$("#slug").removeClasss('is-invalid')
.siblings('p')
.removeClass('invalid-feedback').html("");
}
}
}, error: function(jqXHR, exception){
console.log("Something went wrong");
}
})
2
Answers
does the ‘categories.create’ route display your form? if so, where is the storen route used by the form when it is submitted?
With the way you are going, lets setup what you got for now more cleanly.
Considering this, in your terminal inside the project, you can type:
This will show you all the available routes you have.
To debug if your code works, you can (inside the controller functions) do
dump('Dump.');
ordd('Dump and die.)
.This way you can see if you hit the endpoints you have created.
If you inspect the page and go to the "Network" tab, you can see what response you get
Now, regarding your ajax call. Its not recommended to get routes and parse them into JS (See
url: '{{ route("categories.store") }}'
). And for troubleshooting it, try writing the path manually that you got from theroute:list
.Another tip is that ajax has
$.post()
available.Hopefully, this will set you on the right path.
UPDATE:
I assume your query is run when you register an onclick using jquery as well. As such: