【问题标题】:Call model function with parameter from view in Laravel在 Laravel 中使用参数调用模型函数
【发布时间】: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-&gt;where('tour_id', '=' , $id )-&gt;where('start_year', '=' , $date)-&gt;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 * from dateprice` 其中dateprice.tour_id = 1 和dateprice.tour_id 不为空并且tour_id = 1 和start_year = 2017 由@9876543 订购@asc)`

标签: php laravel eloquent blade laravel-5.5


【解决方案1】:

您不能以这种方式访问​​关系:

$tour-&gt;datePrice()

改为使用魔法属性:

$tour-&gt;date_price

我认为最好使用不同大小写的范围:

$tour-&gt;date_price-&gt;fixedDates(date('Y')

【讨论】:

  • 感谢您的建议,但您的 sn-p 帮助很大。您的代码 sn-p 正在抛出 Call to a member function fixedDates() on null 错误。
  • 也许我对date_price 的大小写风格有误,而您想改用datePrice 之类的。关键是你访问一个神奇的 property 而不是关系 method
【解决方案2】:
@foreach($tour->datePrice()->FixedDates(date('Y'))->get() as $date)

但不要在视图模板中这样做,首先在控制器中准备数据并将其传递给视图:

// controller
view(..)->with(['dates' => $tour->datePrice()->FixedDates(date('Y'))->get()]);

// view
@foreach($dates as $date)

您的代码有点工作的原因是您将Eloquent\Builder 类传递给foreach,因此,它是构造的有效参数,但它没有做什么你显然想要/期待。

【讨论】:

  • 我收到错误SQLSTATE[42S22]: Column not found: 1054 Unknown column 'start' in 'order clause' (SQL: select * from dateprice`,其中dateprice.tour_id = 1 和dateprice.tour_id 不为空且tour_id = 1 和start_year = 2017 年订单startasc)`
  • @TanjaForsberg 好吧,错误说你没有这样的专栏,对吧?我从你的 sn-p 猜想,你宁愿使用start_year/start_month/start_day,顺便说一句,这不是在 SQL 中存储日期的最佳方法;)
  • 找到解决方案我从 Tour.php 中的 dateprice 方法中删除了 -&gt;orderBy('start', 'asc')
猜你喜欢
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2021-06-20
  • 1970-01-01
  • 2021-09-21
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多