1

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

例如,告诉我一个房间,日期4/11/2019和城市london

控制器:

    $test = Property::with('dates','details')->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 ▶}
  ]
}
4

5 回答 5

2

你可以做这样的事情,

$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变量代替4/11/2019.

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

编辑

$property = Property::with(['dates' => function ($query) {
    $query->where('datefield', 'like', '4/11/2019');
}])->get();
于 2019-04-11T12:38:14.903 回答
1

如果要按关系条件过滤实体,则需要whereHas()(请参阅查询关系存在):

$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();

如果您还想通过相同的条件过滤返回的关系,那么您可以在内部进行with()(请参阅约束急切负载):

$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();

您只需要为 执行此操作dates,因为只能有一个details关系,该关系已被限制为'london'

于 2019-04-16T07:17:25.220 回答
1

可能你可以试试

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

而你不需要LIKE。看文档,我不是说LIKE不行,但是使用=orwhereDate会更准确。

你能试一下吗

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

或者

$data = Property::whereHas('dates', function($query){
    $query->where('your_date_field', '4/11/2019');
})->whereHas('city', function($query){
    $query->where('city', 'london');
})->get();
于 2019-04-15T08:56:47.450 回答
0

最重要的是日期字段和城市应该使用相等条件而不是like条件。

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

$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();
于 2019-04-15T04:44:58.427 回答
0

你可以试试这个:

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

如果您只需要属性的详细信息,而不是关系数据,那么您可以尝试:

whereHas 
于 2019-04-16T06:03:05.277 回答