• <legend id='Smn56'><style id='Smn56'><dir id='Smn56'><q id='Smn56'></q></dir></style></legend>
    <tfoot id='Smn56'></tfoot>

      <bdo id='Smn56'></bdo><ul id='Smn56'></ul>

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

      1. <small id='Smn56'></small><noframes id='Smn56'>

        防止 Laravel 将多条记录添加到数据透视表

        时间:2023-09-23
          • <bdo id='NUKlC'></bdo><ul id='NUKlC'></ul>

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

              <tbody id='NUKlC'></tbody>

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

                  本文介绍了防止 Laravel 将多条记录添加到数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我建立了一个多对多的关系并且正在工作,将一个项目添加到我使用的购物车中:

                  I have a many to many relationship set up and working, to add an item to the cart I use:

                  $cart->items()->attach($item);
                  

                  将项目添加到数据透视表(应该如此),但如果用户再次单击链接以添加他们已经添加的项目,则会在数据透视表中创建重复条目.

                  Which adds an item to the pivot table (as it should), but if the user clicks on the link again to add an item they have already added it creates a duplicate entry in the pivot table.

                  是否有一种内置的方法可以在数据透视表不存在的情况下将记录添加到数据透视表中?

                  Is there a built in way to add a record to a pivot table only if one does not already exist?

                  如果没有,我如何检查数据透视表以查找是否已存在匹配的记录?

                  If not, how can I check the pivot table to find if a matching record already exists?

                  推荐答案

                  您可以通过编写一个非常简单的条件来检查现有记录的存在,如下所示:

                  You can check the presence of an existing record by writing a very simple condition like this one :

                  if (! $cart->items->contains($newItem->id)) {
                      $cart->items()->save($newItem);
                  }
                  

                  或者/并且您可以在数据库中添加唯一性条件,它会在尝试保存双峰时抛出异常.

                  Or/and you can add unicity condition in your database, it would throw an exception during an attempt of saving a doublet.

                  您还应该看看下面来自 Barryvdh 的更直接的回答.

                  You should also take a look at the more straightforward answer from Barryvdh just below.

                  这篇关于防止 Laravel 将多条记录添加到数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent ORM 中的多租户 下一篇:Eloquent ->first() 如果 ->exists()

                  相关文章

                  最新文章

                • <small id='GhDDu'></small><noframes id='GhDDu'>

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

                    <tfoot id='GhDDu'></tfoot>
                      • <bdo id='GhDDu'></bdo><ul id='GhDDu'></ul>