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

      <small id='6A3wE'></small><noframes id='6A3wE'>

      1. 检查 belongsToMany 关系是否存在 - Laravel

        时间:2023-10-31
          <bdo id='Hu1Az'></bdo><ul id='Hu1Az'></ul>
            <legend id='Hu1Az'><style id='Hu1Az'><dir id='Hu1Az'><q id='Hu1Az'></q></dir></style></legend>

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

                  <tbody id='Hu1Az'></tbody>

              • <tfoot id='Hu1Az'></tfoot>

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

                  本文介绍了检查 belongsToMany 关系是否存在 - Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的两个表(客户和产品)有一个使用 Laravel 的 blongToMany 和一个数据透视表的多对多关系.现在我想检查某个客户是否有某个产品.

                  Two of my tables (clients and products) have a ManyToMany relation using Laravel's blongToMany and a pivot table. Now I want to check if a certain client has a certain product.

                  我可以创建一个模型来检查数据透视表,但由于 Laravel 不需要该模型用于 belongsToMany 方法,我想知道是否有另一种方法可以在没有数据透视表模型的情况下检查某种关系是否存在.

                  I could create a model to check in the pivot table but since Laravel does not require this model for the belongsToMany method I was wondering if there is another way to check if a certain relationship exists without having a model for the pivot table.

                  推荐答案

                  我觉得官方的做法是这样做的:

                  I think the official way to do this is to do:

                  $client = Client::find(1);
                  $exists = $client->products->contains($product_id);
                  

                  这有点浪费,因为它会执行 SELECT 查询,将所有结果放入 Collection 然后最后执行 foreachCollection 使用您传入的 ID 查找模型.但是,它不需要对数据透视表进行建模.

                  It's somewhat wasteful in that it'll do the SELECT query, get all results into a Collection and then finally do a foreach over the Collection to find a model with the ID you pass in. However, it doesn't require modelling the pivot table.

                  如果您不喜欢这样做的浪费,您可以在 SQL/Query Builder 中自己完成,这也不需要对表进行建模(也不需要获取 Client 模型如果您还没有将其用于其他目的:

                  If you don't like the wastefulness of that, you could do it yourself in SQL/Query Builder, which also wouldn't require modelling the table (nor would it require getting the Client model if you don't already have it for other purposes:

                  $exists = DB::table('client_product')
                      ->whereClientId($client_id)
                      ->whereProductId($product_id)
                      ->count() > 0;
                  

                  这篇关于检查 belongsToMany 关系是否存在 - Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:laravel 包中路由声明的顺序 下一篇:在 xampp 中使用 composer 安装 Laravel 4.1

                  相关文章

                  <tfoot id='ytLG6'></tfoot>

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

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

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