dpp42324 2019-04-11 12:34
浏览 30
已采纳

如何在laravel中的2个关系上使用查询

I have model with 2 relationships. I want to add where condition on each relationship.

For example show me a room that date is 4/11/2019 and the city in london

Controller:

    $test = Property::with('dates','details')->get();

$test result:

It may be a bit long but I expanded whole result so you can check the relations as dates is in a pivot relation :

Collection {#1708 ▼


 #items: array:2 [▼
    0 => Property {#1457 ▼
      #guarded: []
      #connection: "mysql"
      #table: "properties"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:8 [▶]
      #original: array:8 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:2 [▼
        "dates" => Collection {#1607 ▼
          #items: array:1 [▼
            0 => Date {#1600 ▼
              #connection: "mysql"
              #table: "dates"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:7 [▶]
              #original: array:9 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: array:1 [▼
                "pivot" => Pivot {#1602 ▼
                  +incrementing: false
                  #guarded: []
                  #connection: null
                  #table: "date_property"
                  #primaryKey: "id"
                  #keyType: "int"
                  #with: []
                  #withCount: []
                  #perPage: 15
                  +exists: true
                  +wasRecentlyCreated: false
                  #attributes: array:2 [▶]
                  #original: array:2 [▶]
                  #changes: []
                  #casts: []
                  #dates: []
                  #dateFormat: null
                  #appends: []
                  #dispatchesEvents: []
                  #observables: []
                  #relations: []
                  #touches: []
                  +timestamps: false
                  #hidden: []
                  #visible: []
                  #fillable: []
                  +pivotParent: Property {#1461 ▶}
                  #foreignKey: "property_id"
                  #relatedKey: "date_id"
                }
              ]
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }
          ]
        }
        "details" => PropertyDetail {#1702 ▼
          #fillable: array:7 [▶]
          #connection: "mysql"
          #table: "property_details"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:10 [▶]
          #original: array:10 [▼
            "id" => 52
            "property_id" => 65
            "state" => "london"
            "city" => "london"
            "address" => "5"
            "post_code" => 5
            "placearea" => 1
            "telephone" => 5
            "created_at" => "2019-04-09 21:03:10"
            "updated_at" => "2019-04-09 21:03:10"
          ]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #guarded: array:1 [▶]
        }
      ]
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #fillable: []
    }
    1 => Property {#1458 ▶}
  ]
}
  • 写回答

5条回答 默认 最新

  • dry0106 2019-04-16 07:17
    关注

    If you want to filter the entities by conditions on their relations, then you need whereHas() (see Querying Relationship Existence):

    $searchDate = '2019-04-11';
    $searchCity = 'london';
    
    $test = Property::with('dates','details')
            ->whereHas('dates', function($query) use($searchDate) {
                $query->where('date', $searchDate);
            })
            ->whereHas('details', function($query) use($searchCity) {
                $query->where('city', $searchCity);
            })
            ->get();
    

    If you also want to filter the returned relations by the same condition, then you can do it within with() (see Constraining Eager Loads):

    $test = Property::with(['details', 'dates' => function($query) use($searchDate) {
                $query->where('date', $searchDate);
            }])
            ->whereHas('dates', function($query) use($searchDate) {
                $query->where('date', $searchDate);
            })
            ->whereHas('details', function($query) use($searchCity) {
                $query->where('city', $searchCity);
            })
            ->get();
    

    You only need to do that for dates, since there can be only one details relation, which is already constrained to 'london'.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计