【问题标题】:Laravel: Accessors with PaginationLaravel:分页访问器
【发布时间】:2018-02-10 06:59:42
【问题描述】:

我在与 Shops、Restaurants 和 Trips 的多态关系方面有一些提示(对于旅游套餐)。

我正在尝试获取用户给出的所有评论,以及为了什么。

我添加了一个访问器:

public function getHeadlineAttribute($value)
{
  $class = get_class($this->tippable);

  switch ($class){
      case 'App\Shop':
      case 'App\Restaurant':

          return $this->tippable->brand->name;
      case 'App\Trip':
          return $this->tippable->from.' to '.$this->tippable->to;
      default:
          return '';

  }
}

并且想获取如下内容: $user->tips()->with('headline')->paginate(5);

我知道我不能将“with”用于访问器。还有什么办法吗?

【问题讨论】:

    标签: laravel laravel-5 pagination


    【解决方案1】:

    $appends 参数添加到您的Tip 类。你不需要做任何其他事情。

    class Tip extends Model
    {
        protected $appends = ['headline'];
    }
    

    【讨论】:

    • 是的。你比我快 :D
    猜你喜欢
    • 2019-06-18
    • 2018-08-29
    • 2017-11-22
    • 2019-08-29
    • 2018-09-09
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多