Builder.php第2405行中的BadMethodCallException:调用未定义的方法Illuminate \ Database \ Query \ Builder :: answers()

时间:2019-02-20 09:22:16

标签: php mysql laravel web

public function productFaq() {
    return $this->hasMany('App\Models\ProductFaq', 'product_id');
}
public function answers(){
    return $this->hasMany('App\Models\ProductFaqAnswers','faq_id');
}

'productFaq' => function($faq){
    $faq->selectRaw('faq_id,user_id,user_name,product_id,product_name, date_of_enquiry,published,question')
        ->with(['answers' => function($answers)
        {
            $answers->select(['faq_id','answer','user_name','date_of_answer'])
            ->where('published','=',1);
        }
    ]);         
 },

1 个答案:

答案 0 :(得分:1)

您只能在模型上调用with方法,而不能在querybuilder上调用方法,因此它应该是第一条语句,因为当您可以selectRaw时,它将返回一个新的querybuilder实例: / p>

$faq->with(['answers' => function($answers) {
    $answers
        ->select(['faq_id','answer','user_name','date_of_answer'])
        ->where('published','=',1);
 }])
 ->selectRaw('faq_id,user_id,user_name,product_id,product_name,
date_of_enquiry,published,question')->get();