Laravel通过whereHas关系获取ID

时间:2019-07-08 11:05:51

标签: laravel

我正在尝试获取与问题有关系的答案ID的ID,在哪里

    $answers = Question::whereHas('answers',function($q) use($key){
        return $q->where('comment','LIKE','%'.$key.'%')->with('answers.id');
    })->limit(50)->get();

我用->with('answers.id');来解释更多想法,那么我如何获得答案的ID?

2 个答案:

答案 0 :(得分:0)

Project::with(['answers', 'answers.id'])
   ->whereHas('answers', function($q) use($key) {
       return $q->where('comment','LIKE','%'.$key.'%') 
   })->limit(50)
   ->get();

答案 1 :(得分:0)

您尝试过->select()吗?

$answers = Question::whereHas('answers',function($q) use($key){
    return $q->where('comment','LIKE','%'.$key.'%')->select('id');
})->limit(50)->get();