英文:
How i can build the route?
问题
你的问题似乎是在使用Datatables和Laravel开发中遇到的一个路由问题。你尝试使用Ajax来删除数据,但出现了一个URL生成错误。
根据你提供的信息,我理解你需要帮助生成正确的URL。你可以尝试将你的JavaScript代码中的以下部分:
url: "{{ route('admin.messages.destroy', ['user' => auth()->user()->id]) }}/" + id,
替换为:
url: "{{ url('admin/user') }}/{{ auth()->user()->id }}/messages/" + id,
这应该能够生成正确的URL来访问你的删除路由。如果这个解决方案不起作用,请提供更多细节以便我能提供更准确的帮助。
英文:
Hi im building laravel app, i have a tables with messages and i want to display the data with datatables. In my index view the data are visible and the sort, pagination e searchbar of datatables work fine, i also added the edit and delete button, i started working on the delete button but i have a route problem.
this my route file web.php
Route::middleware('auth')
->namespace('Admin')
->name('admin.')
->prefix('admin')
->group(function () {
Route::get('/', 'HomeController@index')->name('home');
Route::resource('/user', 'UserController');/* ->except(['edit', 'update']); */
Route::resource('/user/{user:id}/messages', 'MessagesController');
});
Route::get('messages', 'Admin\MessagesController@getMessages')->name('get.messages');
Auth::routes();
the function in the MessagesController who return me the messages in the datatables
public function getMessages()
{
$user = Auth::user();
return DataTables::of(Message::query())
->addColumn('action', function ($message) use ($user) {
return '<a href="javascript:void(0)" data-toggle="tooltip" data-id="' . $message->id . '" class="btn btn-xs btn-primary" data-toggle="modal" data-target="#edit-modal-' . $message->id . '"><i class="glyphicon glyphicon-edit"></i> Modifica</a>
<a href="javascript:void(0)" class="btn btn-xs btn-danger deleteMessage" data-id="' . $message->id . '" data-user="' . auth()->user()->id . '" data-toggle="modal" data-target="#delete-modal-' . $message->id . '"><i class="glyphicon glyphicon-trash"></i> Elimina</a>
';
})
->setRowClass('{{$id % 2 == 0 ? "alert-success" : "alert-danger"}}')
->setRowId(function ($message) {
return $message->id;
})
->setRowAttr(['align' => 'center'])
->make(true);
}
and this is my jquery code for delete the record in the datatables
$('body').on('click', '.deleteMessage', function() {
var id = $(this).data("id");
confirm("Are You sure want to delete this Post!");
$.ajax({
type: "DELETE",
url: "{{ route('admin.messages.destroy', ['user' => auth()->user()->id]) }}/" +
id,
success: function(data) {
table.draw();
},
error: function(data) {
console.log('Error:', data);
}
});
});
i want the code to delete the record i choose in the datatables but i get this error
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: admin.messages.destroy] [URI: admin/user/{user}/messages/{message}]. (View: C:\Users\ficus\Desktop\Progetti\ArgostudioADV\resources\views\layouts\app.blade.php)
http://127.0.0.1:8000/admin/user/1/messages
someone can find out how i can build the url in the delete function?
i did the change @aynber suggest to me and its worked, i have the vista with the record now, but now i have another problem, when i press the delete button the pop up show i submit but it give me 405 error and the message still there, but if i reload the page the view give me the successful message of record delete and it disappear in the db too, any clue for this?
it is like the function never go in the success but it delete the record because we call the destroy route
答案1
得分: 1
resource route
资源路由
Route::resource('user.messages', MessagesController::class);
message delete link
消息删除链接
<a href="javascript:void(0)" class="btn btn-xs btn-danger deleteMessage" data-route="{{ route('admin.messages.destroy', [auth()->id(), $message->id]) }}" data-toggle="modal" data-target="#delete-modal-{{ $message->id }}"><i class="glyphicon glyphicon-trash"></i> 删除</a>
#js code
#js 代码
$('body').on('click', '.deleteMessage', function() {
var route = $(this).data("route");
confirm("确定要删除这条消息吗!");
$.ajax({
headers: { 'X-CSRF-TOKEN': "{{csrf_token()}}" },
type: "DELETE",
url: route,
success: function(data) {
table.draw();
},
error: function(data) {
console.log('错误:', data);
}
});
});
英文:
First you need to define the resource route correctly.
resource route
Route::resource('user.messages', MessagesController::class);
message delelte link
<a href="javascript:void(0)" class="btn btn-xs btn-danger deleteMessage" data-route="{{ route('admin.messages.destroy', [auth()->id(), $message->id]) }}" data-toggle="modal" data-target="#delete-modal-' . $message->id . '"><i class="glyphicon glyphicon-trash"></i> Elimina</a>
#js code
$('body').on('click', '.deleteMessage', function() {
var route = $(this).data("route");
confirm("Are You sure want to delete this Post!");
$.ajax({
headers: { 'X-CSRF-TOKEN': "{{csrf_token()}}" },
type: "DELETE",
url: route,
success: function(data) {
table.draw();
},
error: function(data) {
console.log('Error:', data);
}
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论