php - Ask about eloquent preloading select
天蓬老师
天蓬老师 2017-05-17 09:55:40
0
1
336

laravel version is 5.4

1, there is such a code, as follows

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

2, the sql statement being executed now is

select * from article where..

I want the sql statement to be executed as

select id,title from user where..

How to implement it

Methods I’ve tried

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

OR

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

Will not work

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
黄舟

I haven’t tried it, but you can refer to this Laravel related query to get only part of the data of the managed object

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template