【发布时间】:2019-11-21 10:03:14
【问题描述】:
我有一个选择国家列表,我需要根据这些值从数据库标志中获取。
这是我的控制器:
if(count($data['user']['career_path']) > 0)
{
$careerpath = array_reverse($data['user']['career_path']->toArray());
$data['company'] = $careerpath[0]['company'];
$data['industry'] = $careerpath[0]['industry']['industry'];
$data['department'] = $careerpath[0]['department']['department'];
$data['job_title'] = $careerpath[0]['functions']['function'];
$flags = \App\Country::lists('flag', 'id');
$flag = $flags->get($careerpath->location);
dd($flags);
}
所以,这是逻辑:
我有一张名为countries 的国家/地区表格。
1.id --- 2.country --- 3.flag
这是我的桌子,我有我所有的国家。
在我看来,我是这样使用这个国家/地区列表的:
{!! Form::select('location',$country_id ,$user->country->id, ['class' => 'js-example-basic-single']) !!}
在哪里$country_id
$data['country_id'] = \App\Country::lists('country','id');
当我从我的选择表单中选择某些内容时,例如希腊或意大利,数据库中会保存该列表中国家/地区的 ID。(20 或 21)
我如何获得该值?喜欢这里:
@foreach($user->career_path as $careerpath)
{{$careerpath->location}}
@endforeach
问题是我需要根据来自location 字段的id 选择country 和flag 值。
所以,如果我的值为 20,我需要根据来自 {{$careerpath->location}} 的值 (id 20) 从表 countries 中获取 country 名称和 flag 值。 $careerpath->location 现在返回我的国家/地区 ID,我需要基于此获取国家/地区名称(来自 countries.country)和标志值(来自 countries.flag)。
【问题讨论】:
-
我没听错吗,您想在选择菜单中的 Italy 或 Greece 旁边添加一个标志,或者是别的什么.不太明白。
-
首先去掉选项值italy中的标签
-
是的。我已经在数据库中拥有每个国家/地区的代码,但这些代码保存在表
countries、列 ID、国家和标志中。在这个值{{$careerpath->location}} I only have the name of countru -
@Aroon 对不起,我写错了。
-
所以
$careerpath->location是一个与countries表中的国家名称相匹配的国家?