简体   繁体   中英

How to check checkbox array values exist from database PHP Laravel?

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 : 国家地区表

Region Table :

区域表

Country Table : 国家表

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>

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');

$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(),
    );

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>

确保 $country_region 是一个数组

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM