Php 如何在for循环中使用elequent将数据存储在变量中
我有一个for循环,我正在其中使用一个查询,但当我尝试获取数据时,它只会显示上次运行该查询的时间,如下所示:Php 如何在for循环中使用elequent将数据存储在变量中,php,mysql,laravel,Php,Mysql,Laravel,我有一个for循环,我正在其中使用一个查询,但当我尝试获取数据时,它只会显示上次运行该查询的时间,如下所示: for ($i = 0; $i < count($dates); $i++) { for ($f = 0; $f < count($room_ids); $f++) { /**************************************** * Looping for
for ($i = 0; $i < count($dates); $i++) {
for ($f = 0; $f < count($room_ids); $f++) {
/****************************************
* Looping for the Number of Rooms User Given
*****************************************/
$room_price = RoomPrice::with('Room')
->where('room_id', $room_ids[$f])
->whereDate('from_date', '<=', $dates[$i])
->whereDate('to_date', '>=', $dates[$i])
->get()->sortBy('created_at');
}
}
for($i=0;$i其中('room\u id',$room\u id[$f])
->whereDate('from_date','=',$dates[$i])
->get()->sortBy('created_at');
}
}
我如何存储$room_price,以便每次运行查询时都能将所有数据存储在集合或其他地方。谢谢启动一个新的集合(在循环之外)
每次你循环得到$room\u价格,你就把它推到$room\u价格
$room_price->push($room_prices);
您可以使用.=存储数据,直到循环结束。检查下面的代码。 对于($i=0;$i
for($f=0;$f其中('room\u id',$room\u id[$f])
->whereDate('from_date','=',$dates[$i])
->get()->sortBy('created_at');
}
}制作
$room\u price
阵列。现在,您正在执行$room\u price=
,这是一种赋值操作,将覆盖以前的值。创建一个数组,通过在循环中调用add item来存储数据。此外,这可能是重复的,也不是检索数据的有效方法。查询试图用from_date
和to_date
做什么?@vivek_23是的,它类似于->whereDate('from_date','=',$dates[$i])
不带to_date
$room_price->push($room_prices);
for ($f = 0; $f < count($room_ids); $f++) {
/****************************************
* Looping for the Number of Rooms User Given
*****************************************/
$room_price .= RoomPrice::with('Room')
->where('room_id', $room_ids[$f])
->whereDate('from_date', '<=', $dates[$i])
->whereDate('to_date', '>=', $dates[$i])
->get()->sortBy('created_at');