When i use route name for url it display this error:
POST http://127.0.0.1:8000/admin/product/%7B%7B%20route(‘product.store’)%20%7D%7D 404 (Not Found)
Instead i have to use /admin/product/store
————————————————————————
main.js
$(document).ready(function(){
$('.form').on('submit', function(e){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
processData: false,
contentType: false,
type: 'POST',
datatype: 'JSON',
data: $(this).serialize(),
url: "{{ route('product.store') }}",
// url: '/admin/product/store'
success: function (result) {
if (result.error == false) {
showToast();
} else {
alert('Thêm lỗi vui lòng thử lại');
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error('AJAX error:', textStatus, errorThrown);
}
});
});
// }
});
web.php
Route::prefix('admin')->group(function () {
Route::controller(ProductController::class)->prefix('product')->group(function () {
Route::get('/list', 'list')->name('product.list')->can('list-product');
Route::get('/add', 'add')->name('product.add')->can('add-product');
Route::post('/store', 'store')->name('product.store');
Route::get('/edit/{id}', 'edit')->name('product.edit')->can('edit-product');
Route::put('/update/{id}', 'update')->name('product.update');
Route::delete('/delete/{id}', 'delete')->name('product.delete')->can('delete-product');
});
});
add.blade.php
@extends('admin.layout.main')
@section('header')
<script src="/ckeditor/ckeditor.js"></script>
@endsection
@section('content')
<form action="" method="GET" class="form">
{{-- @csrf --}}
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="menu">Tên Sản Phẩm</label>
{{-- old(): Trả về giá trị của trường "description" mà người dùng đã nhập trước đó,
để nếu validate error thì sẽ ko cần nhập lại --}}
<input type="text" name="name" id="name" value="{{ old('name') }}" class="form-control" placeholder="Nhập tên sản phẩm">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Danh Mục</label>
<select class="form-control" name="menu_id">
<option value=""></option>
{{-- @foreach($menus as $menu)
<option value="{{ $menu->id }}">{{ $menu->name }}</option>
@endforeach --}}
{!! AppHelpersHelper::recursiveSelectMenu($menus) !!}
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="menu">Giá Gốc</label>
<input type="number" name="price" value="{{ old('price') }}" class="form-control" >
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="menu">Giá Giảm</label>
<input type="number" name="price_sale" value="{{ old('price_sale') }}" class="form-control" >
</div>
</div>
</div>
<div class="form-group">
<label>Mô Tả </label>
<textarea name="description" class="form-control">{{ old('description') }}</textarea>
</div>
<div class="form-group">
<label>Mô Tả Chi Tiết</label>
<textarea name="content" id="content" class="form-control">{{ old('content') }}</textarea>
</div>
<div class="form-group">
<label for="menu">Ảnh Sản Phẩm</label>
<input type="file" class="form-control" name="file" id="upload">
<div id="image_show"></div>
<input type="hidden" name="thumb" id="thumb">
</div>
<div class="form-group">
<label>Kích Hoạt</label>
<div class="custom-control custom-radio">
<input class="custom-control-input" value="1" type="radio" id="active" name="active" checked="">
<label for="active" class="custom-control-label">Có</label>
</div>
<div class="custom-control custom-radio">
<input class="custom-control-input" value="0" type="radio" id="no_active" name="active" >
<label for="no_active" class="custom-control-label">Không</label>
</div>
</div>
</div>
<div class="card-footer">
<button type="submit" name="submit" id="uploadButton" class="btn btn-primary">Thêm Sản Phẩm</button>
</div>
</form>
@endsection
@section('footer')
<script>
CKEDITOR.replace('content');
</script>
@endsection
Does anyone know the problem is ?
New contributor
nam156 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2