2016-03-15 86 views
0

我想創建一個帶有兩個鏈接的下拉菜單。 「刪除」和「編輯」鏈接。laravel方法在href鏈接?

對於刪除功能,我創建了一個表單。

     {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\[email protected]", $comment->id)) !!} 
         {!! Former::danger_submit('Delete') !!} 
         {!! Former::close() !!} 

表單工作,這意味着我的評論被刪除,如果我按下按鈕。

不,我決定刪除刪除按鈕,並使用刪除鏈接做下拉菜單。所以我需要在我的下拉菜單中獲取此表單的邏輯。

但我還沒有在下拉列表中得到了這個..光「刪除」按鈕的下拉的這一部分:

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

但我不能只是把我的控制器功能在「HREF鏈接「,導致沒有'刪除'方法,它不會工作。我希望你們都明白我想說什麼......我的英語不是最好的。

有人可以幫助我嗎?

感謝您的幫助!

我試圖像在此之前,但這沒有工作之一:

<li> 
    <a> 
     {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\[email protected]", $comment->id)) !!} 
     Delete 
     {!! Former::close() !!} 
    </a> 
</li> 

我嘗試直接鏈接到路線:

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

和我的路線是這樣的:

Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroythread'); 

但這並沒有爲我工作..

所有/顯示/路線:

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


Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroythread'); // this is the route we are talking about 
+0

你爲什麼不將其鏈接到使用'@的TestController的destroythread'路線? –

+0

@JilsonThomas請看看我的更新:) – ItzMe488

+0

您使用的是哪個版本的laravel? –

回答

0

Laravel使用方法欺騙做 '刪除', 'PUT', '補丁' 的形式要求。像@Jilson托馬斯提到的,你可以直接創建一個鏈接到路線。我懷疑你在使用足智多謀的路線,這就是爲什麼你要發佈DELETE請求?

看一看路由文檔這一部分,這可能會幫助你:基於https://laravel.com/docs/master/controllers#restful-supplementing-resource-controllers

在你的路由發佈,相信下面的兩個路由匹配它得到你想要的路線了。

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

嘗試在上面移動你想要的路線,看看會發生什麼。

編輯

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

這將產生一個GET請求,因此它不會匹配路線::刪除(...)。以前的方法是張貼表單到路線。此外,將整個表單封裝在錨標記中也是無效的標記。

+0

我會更新我的問題,請在一分鐘內看看它:) – ItzMe488

+0

好吧更新它 – ItzMe488

+0

你有Route :: delete之前定義的任何路由(...)將發佈到'/ show/{id}'?我只是想知道你是否有衝突在你的routes.php – btl

0

因此,根據評論中的討論,您將不得不使用ajax請求從錨標記執行delete請求。

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

,並在您的路線:

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

HTML

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

我在哪裏放這個Ajax代碼? '

  • Delete
  • '多數民衆贊成我的droptdown刪除菜單現在看起來 – ItzMe488

    +0

    將AJAX代碼放入JavaScript文件。 –

    +0

    對不起,我昨天有一些互聯網問題..我現在會試試這個 – ItzMe488

    0

    替代方式,嘗試 'Laravel Collective' 的HTML幫助。

    HTML

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

    routes.php文件

    Route::delete('show/{show}', [ 
        'uses' => '[email protected]', 
        'as' => 'show.destroy' 
    ]); 
    
    +0

    好,但這是一個提交按鈕。我知道做正常按鈕的方式,但我不想在其中刪除部分的下拉菜單。 – ItzMe488