简体   繁体   English

在laravel 5中搜索belongsToMany关系

[英]searching in belongsToMany relationship in laravel 5

Actually i want to search those question which user want to search after select any subject or course.实际上,我想在选择任何主题或课程后搜索用户想要搜索的那些问题。

if a remove either whereHas from subject or course its works but with both its not working.如果从主题或课程中删除whereHas的工作,但两者都不起作用。

Please give a better solution for searching in belongsToMany realtionship.请为在 belongsToMany 关系中搜索提供更好的解决方案。

i have a question table with Question model class我有一个带有Question模型类的问题表

class Question extends Model{
    public function courses(){
        return $this->belongsToMany('App\Models\Course','course_questions');
    }

    public function subjects(){
        return $this->belongsToMany('App\Models\Subject','subject_questions');
    }
}

and in my searchController在我的searchController

public function index(Request $request){
        $questions = Question::with(['user','courses','branches','subjects','years','universities','question_type'])
            ->where("status","=",1)
            ->where(function($query) use($request){
                $q = $request->q;
                if(isset($q) && !is_null($q)){
                    $query->where("question","LIKE","%$q%");
                }
            })
            ->whereHas('subjects',function($query) use($request){
                $subjects = $request->subject;
                if(isset($subjects)){
                    $_subjects = explode(" ",$subjects);
                    $query->whereIn("slug",$_subjects)
                    ->orWhereIn("subject_name",$_subjects);
                }
            })
            ->whereHas('courses',function($query) use($request){
                $course = $request->course;
                if(isset($course)){
                    $_course = explode(" ",$course);
                    $query->whereIn("slug",$_course)
                        ->orWhereIn("course",$_course);
                }
            })
            ->paginate();
        if($request->ajax()){
            $returnHTML = view('questions.question_list')->with('questions', $questions)->render();
            return response()->json(array('success' => true, 'pageContent'=>$returnHTML));
        }

You should build your query probably this way - you should verify conditions before adding any constraints to your query:您应该以这种方式构建查询 - 您应该在向查询添加任何约束之前验证条件:

$query = Question::with(['user','courses','branches','subjects','years','universities','question_type'])
            ->where("status","=",1);

$q = $request->q;
if(isset($q) && !is_null($q)) {
    $query = $query->where("question","LIKE","%$q%");
}

$subjects = $request->subject;
if (isset($subjects)) {
    $query = $query->whereHas('subjects',function($query) use($subjects){                
        $_subjects = explode(" ",$subjects);
        $query->whereIn("slug",$_subjects)
       ->orWhereIn("subject_name",$_subjects);                
   });
}

$course = $request->course;
if (isset($course)) {
    $query = $query->whereHas('courses',function($query) use($course ){       
        $_course = explode(" ",$course);
        $query->whereIn("slug",$_course)
       ->orWhereIn("course",$_course);
    });
}

$questions = $query->paginate();
$products = Product::query()->
    WhereHas('categories', function ($q) use ($keyword) {
        $q->where('products.name', $keyword)
            ->orWhere('categories.name', $keyword);
    })->get();

This is how I have used in my project这就是我在项目中使用的方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM