搜索

首页  >  问答  >  正文

php - 请教关于eloquent预加载select的问题

laravel版本为5.4

1,有个这样的代码,如下

$this->model->with('article')->get()

2,现在执行的sql语句为

select * from article where..

我想sql语句能执行为

select id,title from user where..

请问如何实现

我试过的方法

$this->model->with(['user' => function($query){
        $query->select('id','article');
  }])->get()

OR

public function user() {
    $query->belongTo(Article::class)->select('id', 'title');
}

都不行

天蓬老师天蓬老师2842 天前381

全部回复(1)我来回复

  • 黄舟

    黄舟2017-05-17 09:57:40

    没有试过,不过你可以参考这个Laravel关联查询只获取管理对象的部分数据

    回复
    0
  • 取消回复