我有两个模型
Tour.php
和
Itinerary.php
在一对多关系中:
教程.php
public function itinerary()
{
return $this->hasMany('App\Itinerary', 'tour_id')->orderBy('day', 'asc');
}
行程.php
public function tour()
{
return $this->belongsTo('App\Tour', 'tour_id');
}
这个
itineraries
表由以下列组成:
id | tour | id | day |详细信息
这个
day
&安培;
detail
列为字符串类型。
我想按天的升序打印一次旅行的行程。
如何将字符串更改为整数?
我在视图中用foreach循环打印输出:
@foreach($tour->itineraries as $item)
{{$item->day}}
{{$item->detail}}
@endforeach
我在模型中尝试了cast方法:
protected $casts = [
'day' => 'integer',
];
和访问器
public function castDayToInt($value)
{
return (int)$value;
}
但没有得到预期的结果。
上述代码的电流输出:
-
第一天
-
第11天
-
第12天
-
....
-
第2天
-
第20天
-
第21天
预期产量