【发布时间】:2018-03-21 17:08:18
【问题描述】:
我有两个模型是一对多的关系
Tour.php
class Tour extends Model{
protected $table = 'tours';
public function datePrice()
{
return $this->hasMany('App\DatePrice', 'tour_id')->orderBy('start', 'asc');
}
}
和DatePrice.php
class DatePrice extends Model
{
public $timestamps = false;
protected $table = 'dateprice';
protected $fillable = [
'tour_id',
'start_day',
'start_month',
'start_year',
'end_day',
'end_month',
'end_year',
'price'];
public function tour()
{
return $this->belongsTo('App\Tour', 'tour_id');
}
public function scopeFixedDates($query, $date)
{
return $query->where('start_year', '=' , $date);
}
}
我正在尝试根据作为参数传递给我的 tour.show 路线和视图中的模型函数的年份来获取游览日期。我在 show.blade.php 视图中获取数据的代码是:
@foreach($tour->datePrice()->FixedDates(date('Y')) as $date)
<tr>
<td>
{{ date("jS M, Y", strtotime($date->start_month.'/'.$date->start_day.'/'.$date->start_year )) }}
</td>
<td>
{{ date("jS M, Y", strtotime($date->end_month.'/'.$date->end_day.'/'.$date->end_year )) }}
</td>
</tr>
@endforeach
我在上面的代码中遇到的问题是它没有抛出任何错误消息或给我结果。我有 1 行填充了 2017 年的开始年份。该表应该填充数据,但它是空的。当我在 foreach 循环中转储并死 {{$date}} 时,结果是空白页。如果有人能指出我所犯的错误或建议其他替代方法来替代我想要的输出,我将不胜感激。
【问题讨论】:
-
我在使用您的代码被剪断后收到此错误消息
Allowed memory size of 134217728 bytes exhausted (tried to allocate 62918656 bytes)\t。 -
我已经修改了我的
FixedDates()方法。return $query->where('tour_id', '=' , $id )->where('start_year', '=' , $date)->get();我的dateprice的表结构是id | tour_id | start_day | start_month | start_year | end_day | end_month | end_year。得到了这个错误。 -
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'start' in 'order clause' (SQL: select * fromdateprice` 其中dateprice.tour_id= 1 和dateprice.tour_id不为空并且tour_id= 1 和start_year= 2017 由@9876543 订购@asc)`
标签: php laravel eloquent blade laravel-5.5