I've been trying to create an update status for only one value in my database which is isdisabled
from 0 to 1 by using a AJAX onclick. Here are my codes:
index.blade.php :
$('body').on('click', '.disableMediaOrder', function(){
var id = $(this).data("id");
confirm("Are you sure you want to disable this ?");
$.ajax({
url: "{{ route('media-order.disable') }}" '/' id,
method: "GET",
success: function (data){
tableMediaOrder.draw();
},
error: function (data) {
console.log('Error:', data);
}
});
});
Controller :
public function index(Request $request)
{
if ($request->ajax()) {
$data = MediaOrder::get();
return DataTables::of($data)
->addIndexColumn()
->addColumn('action',function($row){
$btn = '<a href="javascript:void(0)" id="editMediaOrder" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" value="{{ csrf_token() }}">Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" value="{{ csrf_token() }}">Delete</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" id="disableMediaOrder" data-id="'.$row->id.'" data-original-title="Delete" value="{{ csrf_token() }}">Disable</a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
} else {
$groups = Group::all();
$media_order = DB::table('media_order')->get();
$moo = MediaOrder::orderBy('nomor','asc')->get();
$users = User::orderBy('name', 'asc')->get();
return view('media-order.index')->with(compact('users','moo','groups'));
}
}
public function disable(Request $request, $id)
{
DB::unprepared('SET IDENTITY_INSERT media_order ON');
MediaOrder::updateOrCreate(['id'=> $request->id],
[
'isdisabled' => 1,
]);
DB::unprepared('SET IDENTITY_INSERT media_order OFF');
if ($request->ajax()){
return redirect_ajax_notification('media-order.index');
}else{
return redirect_ajax('media-order.index');
}
}
And here is my route:
Route::post('/MediaOrder/disable', 'MediaOrderController@disable')->name('media-order.disable');
And sadly it comes up with 'NotFoundHTTPException;' maybe someone can assist me in solution for this.
CodePudding user response:
You're getting a 404 because you are using a URL which has no route set up:
url: "{{ route('media-order.disable') }}" '/' id,
This generates a URL like /MediaOrder/disable/X
. But you have no route matching that, the one you have will work for /MediaOrder/disable
.
Update your route like this:
Route::post('/MediaOrder/disable/{id}', 'MediaOrderController@disable')->name('media-order.disable');
And update your JS like this (simple option, without using route()
):
url: "/MediaOrder/disable/" id,
If you want to use route()
to generate the URL, you need to do a more complex substitution, since we can't access the JS id
variable from within Blade {{ }}
. You can try:
$('body').on('click', '.disableMediaOrder', function(){
var id = $(this).data("id");
confirm("Are you sure you want to disable this ?");
// First generate the route with a dummy text placeholder
let url = "{{ route('media-order.disable', ['id' => ':id']) }}";
// Now replace the placeholder with the real value
url = url.replace(':id', id);
$.ajax({
url: url,
// ... rest of your code