<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

        Check if belongsToMany relation exists - Laravel(检查 belongsToMany 关系是否存在 - Laravel)
          <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <tfoot id='ytLG6'></tfoot>

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

                          <tbody id='ytLG6'></tbody>

                        <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>