<legend id='b8Cbp'><style id='b8Cbp'><dir id='b8Cbp'><q id='b8Cbp'></q></dir></style></legend>

    <small id='b8Cbp'></small><noframes id='b8Cbp'>

      • <bdo id='b8Cbp'></bdo><ul id='b8Cbp'></ul>
      <i id='b8Cbp'><tr id='b8Cbp'><dt id='b8Cbp'><q id='b8Cbp'><span id='b8Cbp'><b id='b8Cbp'><form id='b8Cbp'><ins id='b8Cbp'></ins><ul id='b8Cbp'></ul><sub id='b8Cbp'></sub></form><legend id='b8Cbp'></legend><bdo id='b8Cbp'><pre id='b8Cbp'><center id='b8Cbp'></center></pre></bdo></b><th id='b8Cbp'></th></span></q></dt></tr></i><div id='b8Cbp'><tfoot id='b8Cbp'></tfoot><dl id='b8Cbp'><fieldset id='b8Cbp'></fieldset></dl></div>

        <tfoot id='b8Cbp'></tfoot>

        laravel Eloquent ORM delete() 方法

        时间:2023-09-23
            <tfoot id='fcZkP'></tfoot>

            <small id='fcZkP'></small><noframes id='fcZkP'>

              <i id='fcZkP'><tr id='fcZkP'><dt id='fcZkP'><q id='fcZkP'><span id='fcZkP'><b id='fcZkP'><form id='fcZkP'><ins id='fcZkP'></ins><ul id='fcZkP'></ul><sub id='fcZkP'></sub></form><legend id='fcZkP'></legend><bdo id='fcZkP'><pre id='fcZkP'><center id='fcZkP'></center></pre></bdo></b><th id='fcZkP'></th></span></q></dt></tr></i><div id='fcZkP'><tfoot id='fcZkP'></tfoot><dl id='fcZkP'><fieldset id='fcZkP'></fieldset></dl></div>
                <tbody id='fcZkP'></tbody>
                <bdo id='fcZkP'></bdo><ul id='fcZkP'></ul>

              • <legend id='fcZkP'><style id='fcZkP'><dir id='fcZkP'><q id='fcZkP'></q></dir></style></legend>
                • 本文介绍了laravel Eloquent ORM delete() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在学习 Laravel.我使用 Eloquent ORM 删除方法,但得到不同的结果.不是真或假而是空.我设置了一个资源路由,在UsersController中有一个destroy方法.

                  Hi I am studying laravel. I use Eloquent ORM delete method but I get a different result.Not true or false but null. I set an resource route and there is a destroy method in UsersController.

                  public function destroy($id){
                  
                    $res=User::find($id)->delete();
                    if ($res){
                      $data=[
                      'status'=>'1',
                      'msg'=>'success'
                    ];
                    }else{
                      $data=[
                      'status'=>'0',
                      'msg'=>'fail'
                    ];
                    return response()->json($data);
                  

                  但是我总是得到响应{"status":"0","msg":"failed"},数据库中的记录被删除了.

                  But I always get a response {"status":"0","msg":"failed"},the record in the database is deleted.

                  然后我使用 dd($res) .它在页面中显示为空.

                  Then I use dd($res) .It shows null in the page.

                  但是从我学习的课程中,它返回一个布尔值 true 或 false.

                  But from the course I learn it returns a boolean value true or false.

                  我的代码有错误吗?

                  你能告诉我从数据库中删除数据时可以得到布尔结果的其他方法吗?

                  Can you tell me some other method that I can get a boolean result when I delete data from database?

                  推荐答案

                  delete 之前,laravel 有几个方法.

                  Before delete , there are several methods in laravel.

                  User::find(1)User::first() 返回一个实例.

                  User::find(1) and User::first() return an instance.

                  User::where('id',1)->getUser::all() 返回一个实例集合.

                  User::where('id',1)->get and User::all() return a collection of instance.

                  在模型实例上调用 delete 将返回 true/false

                  call delete on an model instance will returns true/false

                  $user=User::find(1);
                  $user->delete(); //returns true/false
                  

                  对实例集合调用 delete 将返回一个数字,表示已删除的记录数

                  call delete on a collection of instance will returns a number which represents the number of the records had been deleted

                  //assume you have 10 users, id from 1 to 10;
                  $result=User::where('id','<',11)->delete(); //returns 11 (the number of the records had been deleted)
                  
                  //lets call delete again
                  $result2=User::where('id','<',11)->delete(); //returns 0 (we have already delete the id<11 users, so this time we delete nothing, the result should be the number of the records had been deleted(0)  ) 
                  

                  还有其他删除方法,你可以调用destroy作为模型静态方法,如下所示

                  Also there are other delete methods, you can call destroy as a model static method like below

                  $result=User::destroy(1,2,3);
                  $result=User::destroy([1,2,3]);
                  $result=User::destroy(collect([1, 2, 3]));
                  //these 3 statement do the same thing, delete id =1,2,3 users, returns the number of the records had been deleted
                  
                  

                  还有一点,如果你是 laravel 的新手,你可以使用 php artisan tinker 来查看结果,这样效率更高,然后 dd($result) , print_r($result);

                  One more thing ,if you are new to laravel ,you can use php artisan tinker to see the result, which is more efficient and then dd($result) , print_r($result);

                  这篇关于laravel Eloquent ORM delete() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 获取相关模型的类名 下一篇:Laravel 中的 destroy() 和 delete() 方法有什么区别?

                  相关文章

                  最新文章

                  <tfoot id='0S5wF'></tfoot>
                • <small id='0S5wF'></small><noframes id='0S5wF'>

                    <bdo id='0S5wF'></bdo><ul id='0S5wF'></ul>

                  1. <i id='0S5wF'><tr id='0S5wF'><dt id='0S5wF'><q id='0S5wF'><span id='0S5wF'><b id='0S5wF'><form id='0S5wF'><ins id='0S5wF'></ins><ul id='0S5wF'></ul><sub id='0S5wF'></sub></form><legend id='0S5wF'></legend><bdo id='0S5wF'><pre id='0S5wF'><center id='0S5wF'></center></pre></bdo></b><th id='0S5wF'></th></span></q></dt></tr></i><div id='0S5wF'><tfoot id='0S5wF'></tfoot><dl id='0S5wF'><fieldset id='0S5wF'></fieldset></dl></div>

                      <legend id='0S5wF'><style id='0S5wF'><dir id='0S5wF'><q id='0S5wF'></q></dir></style></legend>