jeudi 9 janvier 2020

WhereIn in Query doesn't work for array, Any solution?

I have defined scopeFilter in Model which has following code

public function scopeFilter($query, array $filters){
    $query->when($filters['subjects'] ?? null, function ($query, $data) {
        //Subjects ids will be comma seperated string eg : 127,125
        $subject_array = explode(',', htmlspecialchars_decode($data))
        $query->where(function ($query) use ($subject_array) {
            $query->whereIn('subjects_ids', $subject_array);
        });
    });
    $query->when($filters['topics'] ?? null, function ($query, $data) {
        //topics ids will be comma seperated string eg : 127,125
        $topic_array = explode(',', htmlspecialchars_decode($data))
        $query->where(function ($query) use ($topic_array) {
            $query->whereIn('topics_ids', $topic_array);
        });
    });
    $query->when($filters['search'] ?? null, function ($query, $search) {
        $query->where(function ($query) use ($search) {
            $query->where('name', 'like', '%'.$search.'%');
        });
    });
}

Here is how I'm using this in controller

$exams = Exams::filter(\Request::only('search', 'subjects','topics'))
            ->orderBy('status', 'DESC')
            ->orderBy('updated_at', 'DESC')->transform(....);

If this is passed in url query then the result will be empty. Am i doing anything wrong here?

Basically the are three filter, search works fine but subjects and topics won't work.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire