简体   繁体   English

href链接中的laravel方法?

[英]laravel method in href link?

I want to create a dropdown with two links.我想创建一个包含两个链接的下拉列表。 A 'Delete' and a 'Edit' link. “删除”和“编辑”链接。

For the delete function I created a form.对于删除功能,我创建了一个表单。

                        {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroythread", $comment->id)) !!}
                        {!! Former::danger_submit('Delete') !!}
                        {!! Former::close() !!}

The form works, that means my comment get's deleted, if I'm pressing the button.该表单有效,这意味着如果我按下按钮,我的评论将被删除。

No I decided to remove the delete button and do a dropdown with a delete link.不,我决定删除删除按钮并使用删除链接进行下拉菜单。 So I need to get the logic of this form in my dropdown menu.所以我需要在我的下拉菜单中获取这个表单的逻辑。

But I haven't got this in the dropdown.. The optical 'Delete' button is this part of the dropdown:但我在下拉列表中没有这个.. 光学“删除”按钮是下拉列表的这一部分:

<li><a href="#">
Delete
</a></li> 

But I can't just put my controller function in that "href-link", cause without the 'DELETE'-Method, it won't work.但我不能只将我的控制器功能放在那个“href-link”中,因为没有“DELETE”方法,它将无法工作。 I hope you all understand what I'm trying to say... my english isn't the best anyway.我希望你们都明白我想说什么……反正我的英语不是最好的。

Can somebody help me with this?有人可以帮我吗?

Thanks for any help!谢谢你的帮助!

I tried it like this before but this haven't worked either:我以前试过这样,但这也没有奏效:

<li>
    <a>
        {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\TestController@destroythread", $comment->id)) !!}
        Delete
        {!! Former::close() !!}
    </a>
</li>

my try linking directly to the route:我尝试直接链接到路线:

<li><a href="{{ route('destroy', $comment->id) }}">Delete</a></li>

and my Route looks like this:我的路线看起来像这样:

Route::delete('/show/{id}', 'Test\\TestController@destroythread')->name('destroythread');

but this haven't worked for me..但这对我不起作用..

all /show/ routes:所有 /show/ 路线:

Route::get('/show/{id}', 'Test\\TestController@show');
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController@update']);
Route::get('/show/{id}/edit', 'Test\\TestController@edit')->name('edit');
Route::delete('/show/{id}', 'Test\\TestController@destroy')->name('destroy');


Route::delete('/show/{id}', 'Test\\TestController@destroythread')->name('destroythread');   // this is the route we are talking about 

Laravel uses method spoofing to do 'DELETE', 'PUT', 'PATCH' form requests. Laravel 使用方法欺骗来执行 'DELETE'、'PUT'、'PATCH' 表单请求。 Like @Jilson Thomas mentioned, you can just create a link directly to the route.就像@Jilson Thomas 提到的那样,您可以直接创建指向路线的链接。 I suspect you're using resourceful routes, thats why you're trying to post a DELETE request?我怀疑您正在使用资源丰富的路线,这就是您尝试发布 DELETE 请求的原因?

Have a look at this section in the routing docs, this may help you out: https://laravel.com/docs/master/controllers#restful-supplementing-resource-controllers查看路由文档中的此部分,这可能会对您有所帮助: https ://laravel.com/docs/master/controllers#restful-supplementing-resource-controllers

Based on your routes posted, I believe the following two routes are matching before it gets to your desired route.根据您发布的路线,我相信以下两条路线在到达您想要的路线之前是匹配的。

Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController@update']);
Route::delete('/show/{id}', 'Test\\TestController@destroy')->name('destroy');

Try moving your desired route above these and see what happens.尝试将您想要的路线移到这些路线之上,看看会发生什么。

Edit编辑

<li><a href="{{ route('destroy', $comment->id) }}">Delete</a></li>

That will produce a GET request, therefore it will not match Route::delete(...).这将产生一个 GET 请求,因此它不会匹配 Route::delete(...)。 The previous method was posting a form to the route.以前的方法是将表单发布到路由。 Also, wrapping an entire form in an anchor tag is invalid markup.此外,将整个表单包装在锚标记中是无效标记。

So, as per the discussion in the comments, you'll have to use ajax request to do a delete request from an anchor tag.因此,根据评论中的讨论,您必须使用 ajax 请求从锚标记执行delete请求。

$.ajax({
    url: '/show/'+$('#testId').attr('value'),
    type: 'DELETE',
    success: function(data){ if(data.success) alert('Deleted'); },
    error: function() {} 
});

and in your route:并在您的路线中:

Route::delete('/show/{id}', ['as'=>'destroy', 'uses'=>'Test\\TestController@destroy']);

HTML HTML

<li><a href="#" id="testId" value="{{$comment->id}}">Delete</a></li>

Alternative way, try ' Laravel Collective ' Html Helper.另一种方法,尝试“ Laravel Collective ”Html Helper。

HTML HTML

{!! Form::open('delete', 
    'method' => 'delete,
    'route'  => ['show.destroy', $comment->id]
) !!}

     {!! Form::submit('Submit') !!}

{!! Form::close() !!}

routes.php路由.php

Route::delete('show/{show}', [
  'uses' => 'TestController@destroy',
  'as' => 'show.destroy'
]);

Try this:尝试这个:

The form with method dont show and you can call a route / url with method POST/PUT/DELETE ...带有方法的表单不显示,您可以使用 POST/PUT/DELETE 方法调用路由/url ...

<a href="{{ route('logout') }}" class="dropdown-item" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">Logout</a>
                  
<form id="logout-form" action="{{ route('item/delete',) }}" method="POST" style="display: none;">
    @csrf
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM