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

Php 如何在laravel中使用对2个关系的查询,php,laravel,multidimensional-array,eloquent,eloquent-relationship,Php,Laravel,Multidimensional Array,Eloquent,Eloquent Relationship,我有两个关系的模型。我想在每个关系上添加where条件 例如,向我展示一个日期为2019年11月4日的房间和伦敦的城市 控制器: $test = Property::with('dates','details')->get(); $测试结果: $property = Property::with(['dates' => function ($query) { $query->where('datefield', 'like', '4/11/2019'); }]

我有两个关系的模型。我想在每个关系上添加where条件

例如,向我展示一个日期为2019年11月4日的房间和伦敦的城市

控制器:

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

$property = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019');
}])->get();
它可能有点长,但我扩展了整个结果,以便您可以检查日期在轴关系中的关系:

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 ▶}
  ]
}

你可以这样做

$data = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019'); // datefield I ain't saw in your output, you can replace it 
}],['details' => function ($query) {
    $query->where('city', 'like', 'london');
}])->get();
dd($data);
请参阅有关如何使用它的文档

我希望表中的日期格式为
m/d/Y
,如果不是,则必须遵循以下步骤

$date = date("Y-m-d",strtotime(str_replace("/","-",$yourdate)));
您可以使用
$date
变量代替
2019年11月4日

注:m/d/y或d-m-y格式的日期通过查找来消除歧义 在各个组件之间的分隔符处:如果分隔符为 斜杠(/),则假定为美式m/d/y;然而,如果 分隔符是破折号(-)或点(.),然后是欧洲d-m-y格式 这是假定的。但是,如果年份以两位数格式给出,并且 分隔符为破折号(-),日期字符串解析为y-m-d

编辑

$property = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019');
}])->get();

你可以这样做

$data = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019'); // datefield I ain't saw in your output, you can replace it 
}],['details' => function ($query) {
    $query->where('city', 'like', 'london');
}])->get();
dd($data);
请参阅有关如何使用它的文档

我希望表中的日期格式为
m/d/Y
,如果不是,则必须遵循以下步骤

$date = date("Y-m-d",strtotime(str_replace("/","-",$yourdate)));
您可以使用
$date
变量代替
2019年11月4日

注:m/d/y或d-m-y格式的日期通过查找来消除歧义 在各个组件之间的分隔符处:如果分隔符为 斜杠(/),则假定为美式m/d/y;而如果 分隔符是破折号(-)或点(.),然后是欧洲d-m-y格式 假设为。但是,如果以两位数格式给出年份,则 分隔符为破折号(-),日期字符串解析为y-m-d

编辑

$property = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019');
}])->get();
最重要的是日期字段和城市应该使用相同的条件,而不是像
条件那样使用相同的条件。

注意:您应该检查日期格式以获得正确的数据输出

$data = Property::with(['dates' => function ($query) {
            $query->where('your_date_field', '=', '4/11/2019');
        }],['details' => function ($query) {
            $query->where('city', '=', 'london');
        }])->get();
您还可以使用
whereDate()
函数来比较日期字段

$data = Property::with(['dates' => function ($query) {
                $query->whereDate('your_date_field', '4/11/2019');
            }],['details' => function ($query) {
                $query->where('city', '=', 'london');
            }])->get();
最重要的是日期字段和城市应该使用相同的条件,而不是像
条件那样使用相同的条件。

注意:您应该检查日期格式以获得正确的数据输出

$data = Property::with(['dates' => function ($query) {
            $query->where('your_date_field', '=', '4/11/2019');
        }],['details' => function ($query) {
            $query->where('city', '=', 'london');
        }])->get();
您还可以使用
whereDate()
函数来比较日期字段

$data = Property::with(['dates' => function ($query) {
                $query->whereDate('your_date_field', '4/11/2019');
            }],['details' => function ($query) {
                $query->where('city', '=', 'london');
            }])->get();
也许你可以试试

$property = Property::with(['dates' => function ($query) {
    $query->whereDate('datefield', '4/11/2019');
}])->get();
你不需要像
这样的
。看,我不是说像
这样的
不起作用,而是说使用
=
日期将更准确

你能试试吗

$data = Property::with(['dates' => function ($query) {
            $query->whereDate('your_date_field', '=', '4/11/2019');
        }],['details' => function ($query) {
            $query->where('city', 'london');
        }])->get();

也许你可以试试

$property = Property::with(['dates' => function ($query) {
    $query->whereDate('datefield', '4/11/2019');
}])->get();
你不需要像
这样的
。看,我不是说像
这样的
不起作用,而是说使用
=
日期将更准确

你能试试吗

$data = Property::with(['dates' => function ($query) {
            $query->whereDate('your_date_field', '=', '4/11/2019');
        }],['details' => function ($query) {
            $query->where('city', 'london');
        }])->get();

您可以尝试以下方法:

$data = Property::with([
    'dates' => function ($query) {
        $query->whereDate('your_date_field', 'formatted_date');
    },
    'details' => function ($query) {
        $query->where('city', '=', 'london');
    }
])->get();
如果您只需要属性的详细信息,而不需要关系数据,则可以尝试:

whereHas 
您可以尝试以下方法:

$data = Property::with([
    'dates' => function ($query) {
        $query->whereDate('your_date_field', 'formatted_date');
    },
    'details' => function ($query) {
        $query->where('city', '=', 'london');
    }
])->get();
如果您只需要属性的详细信息,而不需要关系数据,则可以尝试:

whereHas 

如果要根据实体关系的条件筛选实体,则需要
whereHas()
(请参阅):

如果还希望按相同条件过滤返回的关系,则可以在
with()中进行过滤(请参阅):


您只需要对
日期
执行此操作,因为只有一个
详细信息
关系,该关系已被约束为
'london'
如果您希望根据实体关系的条件筛选实体,则需要
whereHas()
(请参阅):

如果还希望按相同条件过滤返回的关系,则可以在
with()中进行过滤(请参阅):


你只需要在
日期
中这样做,因为只有一个
详细信息
关系,它已经被限制在
“伦敦”

好吧,rahull,我试过了,但没有成功,它将所有属性返回给我,并且过滤器不起作用。不,我只是用relati返回我的所有属性日期,但不详细关系您的意思是,日期条件有效且详细信息仍不有效表示2019年4月11日该条件有效,但伦敦条件无效?不,条件完全无效有点奇怪,但它返回所有属性和条件无效检查我的编辑部分后,您必须更改
datefield
在mysql表中使用您的列名。您必须以
m/d/Y
的格式检查表中的日期值。如果这两个条件都好。它必须根据laravel Documentation不惜任何代价工作。嗯,rahull,我试过了,但没有成功,它会将所有属性返回给我,并且过滤器不起作用。不,我只是返回带有日期关系但不包含细节关系的所有我的财产你是说,日期条件有效且细节仍不有效意味着2019年4月11日该条件有效但伦敦条件无效?不,条件完全无效有点奇怪,但它返回所有财产和条件无效检查我的编辑部分,您必须在mysql表中使用您的列名更改
日期字段
。您必须以
m/d/Y
格式检查表中的日期值。如果这两个条件都符合要求,则必须根据laravel文档进行操作。请关闭
#关系
,并展开
#属性
>日期
集合。然后重新发布转储。请关闭
#关系
并展开
日期
集合的
#属性
。然后重新发布转储。不,它再次将所有属性返回给我,并且不应用where筛选器,但当我将城市或日期更改为其他内容时,我会收到以下错误:天哪,不可能