search

Home  >  Q&A  >  body text

php - Ask about eloquent preloading select

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

天蓬老师天蓬老师2806 days ago364

reply all(1)I'll reply

  • 黄舟

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

    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

    reply
    0
  • Cancelreply