2017-02-13 148 views
0

我想從我名爲post的表中刪除記錄。我在我的視圖中發送一個名爲tag的param來刪除這個標籤中的特定記錄。 所以這裏是這條路線我是刪除我的帖子反對它的「標籤」字段我的路線使用laravel刪除功能刪除記錄功能

Route::get('/delete' , array('as' =>'delete' , 'uses' => '[email protected]')); 

。我的桌子有兩列。一個是標籤等爲內容 我在PostController中刪除溫控功能是

public function deletepost($tag){ 

    $post = post::find($tag); //this is line 28 in my fuction 
    $post->delete(); 
    echo ('record is deleted') ; 
    } 

我是從我的觀點發送標籤,但它給了以下錯誤

ErrorException in Postcontroller.php line 28: 
    Missing argument 1 for 
    App\Http\Controllers\Postcontroller::deletepost() 

回答

1

你的行動應該是這樣的:

use Illuminate\Http\Request; 

public function deletepost(Request $request) // add Request to get the post data 
{ 
    $tagId = $request->input('id'); // here you define $tagId by the post data you send 
    $post = post::find($tagId); 
    if ($post) { 
     $post->delete(); 
     echo ('record is deleted!'); 
    } else { 
     echo 'record not found!'); 
    } 
} 
+0

公共函數deletepost(請求$請求) { $ TAGID = $請求 - >輸入端( '標籤'); $ post = post :: find($ tagId); $ post-> delete($ tagId); echo('record is deleted'); } 通過改變這個followinf錯誤來了 調用成員函數delete()null –

+0

並將'$ tagId = $ request-> input('id');','id'改爲帖子的名稱由發佈請求發送的ID標識符。 –

+0

我認爲在5.3中我們必須使用get方法而不是輸入。但你的邏輯起作用了。謝謝 ,如果我們想刪除自定義基礎上的任何記錄,除主鍵外,我們必須指定我們的條件。 –

0

你講的不是路線期待該參數。 你應該嘗試一下這種方式在你的路由文件:

Route::get('/delete/{tag}' , array('as' =>'delete' , 'uses' => '[email protected]')); 
+0

NotFoundHttpException在RouteCollection.php線161:現在的瀏覽器是顯示這個錯誤 –

1

你,如果你通過它像TAG_ID那麼你傳遞參數爲例必須使用請求在控制器功能內捕獲它。

public function deletepost(Request $request){ 

    $post = post::find($request::get('tag_id')); 
    $post->delete(); 
    echo ('record is deleted'); 
} 
+0

謝謝@gaya你的方法爲我工作了很多。 –

+0

您的歡迎:D Qadeer_Sipra – Gaya