Php 创建多维数组,用于从Laravel数据库检索开放时间
我想为商店的营业时间做一个积垢。我成功地制作了“创建”部分 我制作了一个多维输入数组,然后将其保存到数据库中。基本上,第一个[]从0到6(一周中的7天),第二个[]在一天中的每个时段(0=上午,1=下午)取0或1 创建输入的示例:Php 创建多维数组,用于从Laravel数据库检索开放时间,php,laravel,multidimensional-array,collections,Php,Laravel,Multidimensional Array,Collections,我想为商店的营业时间做一个积垢。我成功地制作了“创建”部分 我制作了一个多维输入数组,然后将其保存到数据库中。基本上,第一个[]从0到6(一周中的7天),第二个[]在一天中的每个时段(0=上午,1=下午)取0或1 创建输入的示例: <tr> <th scope="row" rowspan="2">Lundi</th> <td>Matin</td> <td><input type=
<tr>
<th scope="row" rowspan="2">Lundi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[0][0][open_at]"></td>
<td><input type="time" class="form-control" name="hours[0][0][closed_at]"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[0][1][open_at]"></td>
<td><input type="time" class="form-control" name="hours[0][1][closed_at]"></td>
</tr>
我的问题是做相反的操作:将数据库数据转换成相同的多维数组,以便重用视图和类似控制器的数据
{{ $hours[0][1]["open_at"] }}
我确实成功地检索到了数组中的好数据,但是数组中的第一个键丢失了,所以我得到了
Undefined offset: 1
以下是我如何检索多维数组上的数据:
$collection = $shop->shops_hour()->get()->makeVisible(['day','period']);
$grouped = $collection->groupBy('day')->groupBy('period');
$hours = $grouped->toArray();
return view('shops.edit',compact('shop','id'))->with('taxonomies', $taxonomies)->with('hours',$hours);
返回的数组:
array:1 [▼
"" => array:7 [▼
0 => array:2 [▼
0 => array:4 [▼
"day" => 0
"period" => 0
"open_at" => "08:00:00"
"closed_at" => "11:30:00"
]
1 => array:4 [▼
"day" => 0
"period" => 1
"open_at" => "12:00:00"
"closed_at" => "14:00:00"
]
]
我知道
提前谢谢你 我在
Shop
模型中添加了hours
方法:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Shop extends Model
{
protected $table='shops';
public $timestamps=false;
public function hours()
{
return $this->hasMany(Shops_hour::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Shops_hour extends Model
{
protected $table = 'shops_hours';
protected $fillable = ['open_at', 'closed_at',];
# I commented this line
#protected $hidden = ['shop_id', 'id', 'day', 'period', 'created_at', 'updated_at',];
public function shop()
{
return $this->belongsTo(Shop::class);
}
}
这是我的路线。例如,我想用ID=1
编辑shop
:
Route::get('/test', function (Request $request) {
$shop=\App\Shop::with('hours')->where('id',1)->first();
return view('shops.edit',compact('shop'));
});
商店。编辑
查看:
<p>shop_id={{$shop->id}}</p>
<p>shop name = {{$shop->name}}</p>
<table>
@foreach($shop->hours as $hour)
<tr>
<th scope="row" >Lundi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[{{$hour->day}}][{{$hour->period}}][open_at]" value="{{$hour->open_at}}"></td>
<td><input type="time" class="form-control" name="hours[{{$hour->day}}][{{$hour->period}}][closed_at]" value="{{$hour->closed_at}}"></td>
</tr>
@endforeach
</table>
shop_id={{{$shop->id}
店铺名称={{$shop->name}
@foreach($shop->小时为$hour)
伦迪
马汀
@endforeach
结果:
shop\u id=1
店铺名称=店铺1
伦迪
马汀
伦迪
马汀
伦迪
马汀
通过替换我的2个groupBy,我成功地使我的阵列几乎达到了我想要的效果
$grouped = $collection->groupBy('day')->groupBy('period');
通过数组
$grouped = $collection->groupBy(['day','period']);
但是,数组给了我第三个[0]索引,总是0(因为我的表(?)上没有重复的数据)
我想要的结果是:一个固定表,其中包含一周中的所有天(法语),每一天的时段(am/pm),以及与给定日期/时段组合关联的正确数据
该代码可以完成这项工作,但我认为它应该更好,并且/或者没有第三个不需要的[0]
<table class="table table-sm">
<thead>
<tr>
<th scope="col">Jour</th>
<th scope="col">Pause</th>
<th scope="col">Ouvert de</th>
<th scope="col">à</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" rowspan="2">Lundi</th>
<td>Matin</td>
<td><input type="text" class="form-control" name="hours[0][0][open_at]" value="{{ $hours[0][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[0][0][closed_at]" value="{{ $hours[0][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[0][1][open_at]" value="{{ $hours[0][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[0][1][closed_at]" value="{{ $hours[0][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Mardi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[1][0][open_at]" value="{{ $hours[1][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[1][0][closed_at]" value="{{ $hours[1][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[1][1][open_at]" value="{{ $hours[1][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[1][1][closed_at]" value="{{ $hours[1][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Mercredi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[2][0][open_at]" value="{{ $hours[2][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[2][0][closed_at]" value="{{ $hours[2][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[2][1][open_at]" value="{{ $hours[2][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[2][1][closed_at]" value="{{ $hours[2][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Jeudi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[3][0][open_at]" value="{{ $hours[3][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[3][0][closed_at]" value="{{ $hours[3][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[3][1][open_at]" value="{{ $hours[3][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[3][1][closed_at]" value="{{ $hours[3][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Vendredi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[4][0][open_at]" value="{{ $hours[4][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[4][0][closed_at]" value="{{ $hours[4][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[4][1][open_at]" value="{{ $hours[4][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[4][1][closed_at]" value="{{ $hours[4][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Samedi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[5][0][open_at]" value="{{ $hours[5][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[5][0][closed_at]" value="{{ $hours[5][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[5][1][open_at]" value="{{ $hours[5][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[5][1][closed_at]" value="{{ $hours[5][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Dimanche</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[6][0][open_at]" value="{{ $hours[6][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[6][0][closed_at]" value="{{ $hours[6][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[6][1][open_at]" value="{{ $hours[6][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[6][1][closed_at]" value="{{ $hours[6][1][0]['closed_at'] }}"></td>
</tr>
</tbody>
</table>
熟练工人
暂停
奥维特德酒店
à
伦迪
马汀
四月中旬
马尔迪
马汀
四月中旬
Mercredi
马汀
四月中旬
朱迪
马汀
四月中旬
文德雷迪
马汀
四月中旬
萨梅迪
马汀
四月中旬
迪芒什
马汀
四月中旬
你能展示商店时间方法吗?@Alihosseinshahabi类商店时间扩展模型{protected$table='shops\u hours';protected$filleble=['open\u at'、'closed\u at',];protected$hidden=['shop\u id','id','day','period','created\u at','updated\u at',];公共功能商店(){return$this->belongsTo('App\Shop');}}?好的。请显示makeVisible
这是我的第一篇文章^^(编辑)谢谢!这看起来比我的方法更好。但是,这并不能解决我的问题(或者我看不到)。我正在尝试为开放时间制作一个完整的表格(一周中的所有天数以及该表中每天的两个时段)我想创建一个数组,以便手动为每个html磁贴分配每个值,例如“Lundi>Matin”将特别指定为“hours[0][0]”,“Lundi>après-midi”是“hours[0][1]”。我的代码返回所有小时数
属于一家商店
。我不明白您到底需要什么。请解释更多。使用您的代码,如果我有重复/错误的数据,每个用户都会看到。@Alihosseinshahabi
<table class="table table-sm">
<thead>
<tr>
<th scope="col">Jour</th>
<th scope="col">Pause</th>
<th scope="col">Ouvert de</th>
<th scope="col">à</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row" rowspan="2">Lundi</th>
<td>Matin</td>
<td><input type="text" class="form-control" name="hours[0][0][open_at]" value="{{ $hours[0][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[0][0][closed_at]" value="{{ $hours[0][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[0][1][open_at]" value="{{ $hours[0][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[0][1][closed_at]" value="{{ $hours[0][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Mardi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[1][0][open_at]" value="{{ $hours[1][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[1][0][closed_at]" value="{{ $hours[1][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[1][1][open_at]" value="{{ $hours[1][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[1][1][closed_at]" value="{{ $hours[1][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Mercredi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[2][0][open_at]" value="{{ $hours[2][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[2][0][closed_at]" value="{{ $hours[2][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[2][1][open_at]" value="{{ $hours[2][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[2][1][closed_at]" value="{{ $hours[2][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Jeudi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[3][0][open_at]" value="{{ $hours[3][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[3][0][closed_at]" value="{{ $hours[3][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[3][1][open_at]" value="{{ $hours[3][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[3][1][closed_at]" value="{{ $hours[3][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Vendredi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[4][0][open_at]" value="{{ $hours[4][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[4][0][closed_at]" value="{{ $hours[4][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[4][1][open_at]" value="{{ $hours[4][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[4][1][closed_at]" value="{{ $hours[4][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Samedi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[5][0][open_at]" value="{{ $hours[5][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[5][0][closed_at]" value="{{ $hours[5][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[5][1][open_at]" value="{{ $hours[5][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[5][1][closed_at]" value="{{ $hours[5][1][0]['closed_at'] }}"></td>
</tr>
<tr>
<th scope="row" rowspan="2">Dimanche</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[6][0][open_at]" value="{{ $hours[6][0][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[6][0][closed_at]" value="{{ $hours[6][0][0]['closed_at'] }}"></td>
</tr>
<tr>
<td>Après-midi</td>
<td><input type="time" class="form-control" name="hours[6][1][open_at]" value="{{ $hours[6][1][0]['open_at'] }}"></td>
<td><input type="time" class="form-control" name="hours[6][1][closed_at]" value="{{ $hours[6][1][0]['closed_at'] }}"></td>
</tr>
</tbody>
</table>