farshadf
5 months ago

how to use queries on 2 relationships in laravel

Posted 5 months ago by farshadf

i have a model and 2 relationships i want to do a where on each relationship so for example show me a room that date is 4/11/2019 and the city in london : here is my code : my controller :


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

and here is a dd of my $test and sorry it may be a bit long but i expand all for you to see the relations because dates is 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 ▶}
      ]
    }

Please sign in or create an account to participate in this conversation.