0

I have an issue where I'm trying to match 2 arrays (Check if $_POST array has values that are contained in the database array)

  1. Array : ['79','80'] (this is stored inside my database)
  2. Array : ['78','79','80'] (this is a POST from checkbox's)

Here's a snippet of my code

$filter = Course::GetAllCourses()->where('for_sites',2)->orderBy("set_date","DESC");
// I've tried
if ($request->getParam('level')) {

    $filter->where('groups',$request->getParam('level'));

}

// Also tried this resukt: nothing shows (no errors ) this works for a single value only though
foreach ($request->getParam('level') as $levels) {

    if ($request->getParam('level')) {

        $filter->where('groups','LIKE','%' . $levels . '%');

    }
}

// Also tried this shows everything doesn't matter what kind of 'level ' I've posted 

foreach ($request->getParam('level') as $levels) {

    if ($request->getParam('level')) {

        $filter->orWhere('groups','LIKE','%' . $levels . '%');

    }
}

$db_course = $filter->distinct()->get();

So the end game is:

If i $_POST values of array ['78','79','80'] i want to get results that have ['78','79'] or all three values or even just 1 blue e.g. ['78'] is this even possible?

2
  • Can you post the result of dd($request->getParam('level') and what exactly is in the groups column of your database? A text string? Commented Dec 12, 2018 at 15:44
  • Take a look at whereJsonContains(): laravel.com/docs/queries#json-where-clauses In your case, you would need multiple orWhereJsonContains() clauses. Commented Dec 12, 2018 at 16:13

1 Answer 1

-1

i think you are searching for WhereIn

Something like :

$users = DB::table('users')
            ->whereIn('id', [1, 2, 3])
            ->get();

Reference

Sign up to request clarification or add additional context in comments.

2 Comments

This works if the Database column is a single value e.g. "1" but my database column contains an array ['1','2','3']
then probably you might looking for this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.