title: How to check checkbox array values exist from database PHP Laravel?
body: <p>I want to check either the countries exist in countries and the checkbox will be displayed. The values is directly from the database. I can't set the value at the input since it is array/multiple values. So, I want to check every array so that it will be a checkbox. </p>
  
  <p><strong>Controller :</strong></p>
  
  <pre><code>public function edit($id)
  {
      $region = Region::find($id);
      $countries = Country::all();
      $country_region = DB::table('regions')
          -&gt;select('countries.id', 'countries.name')
          -&gt;join('country_region', 'regions.id', '=', 'country_region.region_id')
          -&gt;join('countries', 'country_region.country_id', '=', 'countries.id')
          -&gt;where('regions.id', '=', $id)
          -&gt;get();
  
      $items = array(
          'region' =&gt; $region,
          'countries' =&gt; $countries,
          'country_region' =&gt; $country_region,
      );
  
      return view('admin.region.edit')-&gt;with($items);
  }
  </code></pre>
  
  <p><strong>blade</strong></p>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;label&gt;Choose Country&lt;/label&gt;
      &lt;select id="test" type="checkbox" name="country_id[]"&gt;
          @foreach ($countries as $item)
          &lt;option value="{{$item-&gt;id}}" selected @if (in_array($item-&gt;id, array_keys($country_region))) checked="checked"
              @endif&gt;{{$item-&gt;name}}
          &lt;/option&gt;
          @endforeach
      &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>As you can see, I put php statement inside the blade to check either $country_region is exist in the countries or not. I got errors that related to the array such below : </p>
  
  <pre><code>array_keys() expects parameter 1 to be array, object given 
  </code></pre>
  
  <p>Database :</p>
  
  <p>Country Region Table :
  <a href="https://i.stack.imgur.com/8rysg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8rysg.png" alt="Country Region Table"></a></p>
  
  <p>Region Table :</p>
  
  <p><a href="https://i.stack.imgur.com/VV7VF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VV7VF.png" alt="Region Table"></a></p>
  
  <p>Country Table :
  <a href="https://i.stack.imgur.com/QjSEt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QjSEt.png" alt="Country Table"></a></p>