为数组laravel中的值建立关系



我有一个数据表:

Schema::create('general', function (Blueprint $table) {
$table->id();
$table->string('key')->unique();
$table->longText('value')->nullable();
$table->timestamps();
});

添加数据时,我在数据库中得到以下记录:

id:2
键:国家
值:["意大利","德国"]

国家/地区现在通过标签添加到我,如下所示:

$form->tags('value', __('Value'))->help('Use key `<b>Enter</b>` to add a new value')
->separators([';']);

模型具有接收并共享所有值的功能​​与关键国家:

public static function getCountries()
{
$country= self::where('key', 'country')->first();
return explode(',', $country['value']);
}

然后在blade.php页面上,我显示了这些国家:

@foreach(AppModelsGeneral::getCountries() as $country)
<span>{{ $country }}</span>
@endforeach

任务是为每个国家附上一张带有国旗的图片。

我创建了一个带有迁移的新模型来添加图片:

Schema::create('general_flags', function (Blueprint $table) {
$table->id();
$table->string('flag_image', 128);
$table->timestamps();
});

我的控制器都做得很好,添加并保存了图片。

主要问题是如何创建数组中每个值与所需标志的关系,有什么想法吗?

问题是,我无法更改添加的国家,所以我必须使用我现有的。

您可以在general_flags中创建名为country_code的新列,然后在general中保存countries_array时将其保存为关联数组['country_code' => 'country name']。或者将图像保存为关联数组['country_code' => 'Image']
但是,在我看来,你们应该为各国制作一张桌子,每个国家都有一面国旗。

最新更新