【问题标题】:Laravel5.3:How to use pluck in relationship for bindign Form::select element?Laravel 5.3:如何在关系中使用 pluck 来绑定 Form::select 元素?
【发布时间】:2017-08-01 09:31:48
【问题描述】:

这是我的模型:

class Positions extends Model implements Repository
{
    protected $fillable = ['index_id', 'title', 'description'];

    public function index()
    {
        return $this->belongsTo('TEST\Indices', 'index_id');
    }

    public function getById($id)
    {
        return $this->with('index')->find($id);
    }
}

如何在getById()函数中使用pluck()列出索引关系?

【问题讨论】:

  • index 关系中您要提取的字段是什么?

标签: laravel-5 relationship pluck


【解决方案1】:

你可以这样做:

return $this->with('index')->find($id)->pluck('index.indexField');

【讨论】:

  • return $this->with('index')->find($id)->pluck('index.id', 'index.code');//错误:SQLSTATE[42S22 ]: Column not found: 1054 Unknown column 'index.id' in 'field list' (SQL: select index.id from positions)
  • 你能打印出调用getById后得到的结构吗?
猜你喜欢
  • 1970-01-01
  • 2021-12-01
  • 2017-05-31
  • 2018-04-29
  • 2017-02-16
  • 2013-07-13
  • 2017-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多