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?

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

1 Answers1

-1

i think you are searching for WhereIn

Something like :

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

Reference

H45H
  • 1,019
  • 10
  • 28
  • This works if the Database column is a single value e.g. "1" but my database column contains an array ['1','2','3'] – Frosty Dec 12 '18 at 13:24
  • then probably you might looking for [this](https://stackoverflow.com/questions/2981280/php-mysql-mysql-substitute-for-php-in-array-function) – H45H Dec 12 '18 at 13:32