使用laravel删除功能删除记录功能

问题描述:

我想从我名为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() 

你的行动应该是这样的:

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方法而不是输入。但你的逻辑起作用了。谢谢 ,如果我们想删除自定义基础上的任何记录,除主键外,我们必须指定我们的条件。 –

你讲的不是路线期待该参数。 你应该尝试一下这种方式在你的路由文件:

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

NotFoundHttpException在RouteCollection.php线161:现在的浏览器是显示这个错误 –

你,如果你通过它像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