我正在为商店的营业时间制作CRUD。我已经成功制作了“创建”部分。
我输入了一个多维数组,然后将其保存到数据库中。基本上,第一个[]从0到6(一周的7天),第二个[]在一天的每个时间段(0 = AM,1 = PM)取0或1
示例创建输入:
<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>
示例创建表:
id shop_id day period open_at closed_at created_at updated_at
29 1 0 0 08:00:00 11:30:00 2018-06-21 15:23:44 2018-06-21 15:23:44
30 1 0 1 12:00:00 14:00:00 2018-06-21 15:23:44 2018-06-21 15:23:44
31 1 1 0 09:00:00 12:00:00 2018-06-21 15:23:44 2018-06-21 15:23:44
32 1 1 1 13:00:00 16:00:00 2018-06-21 15:23:44 2018-06-21 15:23:44
33 1 2 0 07:00:00 09:00:00 2018-06-21 15:23:44 2018-06-21 15:23:44
我的问题是进行逆运算:将数据库数据转换为相同的多维数组,以便在视图和控制器上重用这些数据,例如
{{ $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"
]
]
我知道 预先谢谢您!
答案 0 :(得分:0)
我在hours
模型中添加了Shop
方法:
<?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);
}
}
Shops_hour
型号:
<?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);
}
}
这是我的路线。例如,我想使用shop
编辑ID=1
:
Route::get('/test', function (Request $request) {
$shop=\App\Shop::with('hours')->where('id',1)->first();
return view('shops.edit',compact('shop'));
});
shops.edit
视图:
<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>
结果:
<p>shop_id=1</p>
<p>shop name = shop1</p>
<table>
<tr>
<th scope="row" >Lundi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[0][0][open_at]" value="08:00:00"></td>
<td><input type="time" class="form-control" name="hours[0][0][closed_at]" value="11:30:00"></td>
</tr>
<tr>
<th scope="row" >Lundi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[0][1][open_at]" value="12:00:00"></td>
<td><input type="time" class="form-control" name="hours[0][1][closed_at]" value="14:00:00"></td>
</tr>
<tr>
<th scope="row" >Lundi</th>
<td>Matin</td>
<td><input type="time" class="form-control" name="hours[1][0][open_at]" value="09:00:00"></td>
<td><input type="time" class="form-control" name="hours[1][0][closed_at]" value="12:00:00"></td>
</tr>
</table>
答案 1 :(得分:0)
我已通过替换2个groupBy来使阵列几乎像我想要的那样
data.sort(function(a, b) {
return a.Test.createDate - b.Test.createDate;
});
按数组
$grouped = $collection->groupBy('day')->groupBy('period');
但是,数组为我提供了第三个[0]索引,始终为0(因为我的表(?)上没有重复的数据)。
我想要的结果是:一个修复表,其中包含一周中的所有天(法语),每天的两个时间段(上午/下午)以及与该给定的日期/时间段相关的正确数据组合。
该代码可以完成工作,但是我认为它应该更好,并且/或者没有第三个不需要的[0]。
$grouped = $collection->groupBy(['day','period']);