3

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.

Controller :

public function edit($id)
{
    $region = Region::find($id);
    $countries = Country::all();
    $country_region = DB::table('regions')
        ->select('countries.id', 'countries.name')
        ->join('country_region', 'regions.id', '=', 'country_region.region_id')
        ->join('countries', 'country_region.country_id', '=', 'countries.id')
        ->where('regions.id', '=', $id)
        ->get();

    $items = array(
        'region' => $region,
        'countries' => $countries,
        'country_region' => $country_region,
    );

    return view('admin.region.edit')->with($items);
}

blade

<div class="form-group">
    <label>Choose Country</label>
    <select id="test" type="checkbox" name="country_id[]">
        @foreach ($countries as $item)
        <option value="{{$item->id}}" selected @if (in_array($item->id, array_keys($country_region))) checked="checked"
            @endif>{{$item->name}}
        </option>
        @endforeach
    </select>
</div>

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 :

array_keys() expects parameter 1 to be array, object given 

Database :

Country Region Table : Country Region Table

Region Table :

Region Table

Country Table : Country Table

6
  • $item->id is an object but you put that on in_array() method
    – Moshiur
    Commented Feb 20, 2020 at 5:03
  • because $item->id also an array inside the $country_region. @Moshiur
    – asad
    Commented Feb 20, 2020 at 5:10
  • 1
    are you trying to make an option selected? then why are you adding checked attribute? it is used in checkbox but you are using it in selectbox
    – Moshiur
    Commented Feb 20, 2020 at 5:19
  • i used checkbox already before but still not work
    – asad
    Commented Feb 20, 2020 at 5:44
  • you must add it outside of the selectbox
    – Moshiur
    Commented Feb 20, 2020 at 5:46

5 Answers 5

0

Use pluck instead of select in the query.

$country_region = DB::table('regions') 
     ->join('country_region', 'regions.id', '=', 'country_region.region_id')
     ->join('countries', 'country_region.country_id', '=', 'countries.id')
     ->where('regions.id', '=', $id)
     ->get()
     ->pluck('countries.name', 'countries.id');
2
  • why I have to use pluck instead of select?
    – asad
    Commented Feb 20, 2020 at 5:09
  • You have used in_array that is the reason you need an array to compare the value and the pluck method returns an array
    – souravmsh
    Commented Feb 20, 2020 at 5:22
0

I don't think you need array function there like in_array since you are already looping each country, simple if should work, also please show the content of tables you are trying to compare.

<div class="form-group">
    <label>Choose Country</label>
    <select id="test" type="checkbox" name="country_id[]">
        @foreach ($countries as $item)
        <option value="{{$item->id}}" selected @if ($item->id == $country_region.country.id) checked="checked"
            @endif>{{$item->name}}
        </option>
        @endforeach
    </select>
</div>
2
  • what do you mean the content of tables? you mean the database?
    – asad
    Commented Feb 20, 2020 at 3:58
  • Yes, content of tables like Country, Country_Region Commented Feb 20, 2020 at 4:02
0

$country_region is not an array in your controller. It's an Eloquent collection. To change it to array you simply use toArray() on it.

In your controller, replace this:

$items = array(
            'region' => $region,
            'countries' => $countries,
            'country_region' => $country_region,
    );

with this:

$items = array(
            'region' => $region,
            'countries' => $countries,
            'country_region' => $country_region->toArray(),
    );
1
  • @asad Updated my answer.
    – Qumber
    Commented Feb 20, 2020 at 6:12
0

Use pluck to get all country's id in an array

$country_region = DB::table('regions')
    ->join('country_region', 'regions.id', '=', 'country_region.region_id')
    ->join('countries', 'country_region.country_id', '=', 'countries.id')
    ->where('regions.id', '=', $id)
    ->pluck('country_region.id')
    ->toArray();

Then don't use array_keys() anymore. Modify your HTML like this

<option value="{{$item->id}}" selected @if (in_array($item->id, $country_region)) checked="checked"
    @endif>{{$item->name}}
</option>
-1

make sure that $country_region is an array

1
  • This statement is more appropriate for comments, not for answers. Please elaborate on it.
    – Qumber
    Commented Feb 20, 2020 at 5:27

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.