如何构建路由?

huangapple go评论59阅读模式
英文:

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(&#39;user.messages&#39;, MessagesController::class);

message delelte link

&lt;a href=&quot;javascript:void(0)&quot; class=&quot;btn btn-xs btn-danger deleteMessage&quot; data-route=&quot;{{ route(&#39;admin.messages.destroy&#39;, [auth()-&gt;id(), $message-&gt;id]) }}&quot;  data-toggle=&quot;modal&quot; data-target=&quot;#delete-modal-&#39; . $message-&gt;id . &#39;&quot;&gt;&lt;i class=&quot;glyphicon glyphicon-trash&quot;&gt;&lt;/i&gt; Elimina&lt;/a&gt;

#js code

$(&#39;body&#39;).on(&#39;click&#39;, &#39;.deleteMessage&#39;, function() {
        var route = $(this).data(&quot;route&quot;);
        confirm(&quot;Are You sure want to delete this Post!&quot;);
        $.ajax({
            headers: { &#39;X-CSRF-TOKEN&#39;: &quot;{{csrf_token()}}&quot; },
            type: &quot;DELETE&quot;,
            url: route,
            success: function(data) {
                table.draw();
            },
            error: function(data) {
                console.log(&#39;Error:&#39;, data);
            }
        });
    });

huangapple
  • 本文由 发表于 2023年2月14日 21:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448385.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定