【问题标题】:Delete a record using laravel delete function使用 laravel 删除功能删除记录
【发布时间】:2017-04-05 16:20:39
【问题描述】:

我想从名为 post 的表中删除一条记录。在我看来,我正在发送一个名为 tag 的参数,以删除针对该标记的某个记录。 所以这是我的路线

   Route::get('/delete' , array('as' =>'delete' , 'uses' => 'Postcontroller@deletepost'));

通过这条路线,我将针对它的“标签”字段删除我的帖子。我的桌子有两列。一个是标签,另一个是内容 我在 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()

【问题讨论】:

    标签: php laravel-5.3


    【解决方案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!');
        }
    }
    

    【讨论】:

    • 公共函数 deletepost(Request $request) { $tagId = $request->input('tag'); $post = post::find($tagId); $post->delete($tagId); echo ('记录被删除') ; } 通过更改此 followinf 错误来调用 null 上的成员函数 delete()
    • 并将$tagId = $request->input('id');id改为post请求发送的post id标识符的名称。
    • 我认为在 5.3 中我们必须使用 get 方法而不是输入。但你的逻辑奏效了。谢谢,如果我们想删除自定义基础上的任何记录,除了主键之外,我们必须指定我们的条件。
    • 实际上我从 5.4 开始就在玩 Laravel,但我很高兴你找到了解决方案。
    【解决方案2】:

    如果您像 tag_id 一样传递参数,则必须传递示例参数,然后您必须使用 Request 在控制器函数中捕获它。

    public function deletepost(Request $request){
    
       $post = post::find($request::get('tag_id')); 
       $post->delete();
       echo ('record is deleted');
    }
    

    【讨论】:

    • 欢迎您:D Qadeer_Sipra
    【解决方案3】:

    你没有告诉路由期望那个参数。 你应该在你的路由文件中尝试这种方式:

    Route::get('/delete/{tag}' , array('as' =>'delete' , 'uses' => 'Postcontroller@deletepost'));
    

    【讨论】:

    • RouteCollection.php 第 161 行中的 NotFoundHttpException:现在浏览器显示此错误
    猜你喜欢
    • 1970-01-01
    • 2018-07-02
    • 2017-03-18
    • 2018-11-08
    • 2012-12-23
    • 2020-01-04
    • 2018-06-11
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多