【问题标题】:How to get flag based on country value?如何根据国家价值获得国旗?
【发布时间】: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 选择countryflag 值。

所以,如果我的值为 20,我需要根据来自 {{$careerpath->location}} 的值 (id 20) 从表 countries 中获取 country 名称和 flag 值。 $careerpath->location 现在返回我的国家/地区 ID,我需要基于此获取国家/地区名称(来自 countries.country)和标志值(来自 countries.flag)。

【问题讨论】:

  • 我没听错吗,您想在选择菜单中的 ItalyGreece 旁边添加一个标志,或者是别的什么.不太明白。
  • 首先去掉选项值italy中的标签
  • 是的。我已经在数据库中拥有每个国家/地区的代码,但这些代码保存在表 countries、列 ID、国家和标志中。在这个值{{$careerpath->location}} I only have the name of countru
  • @Aroon 对不起,我写错了。
  • 所以$careerpath->location 是一个与countries 表中的国家名称相匹配的国家?

标签: php database laravel


【解决方案1】:

如果您在$careerpath->location 中有一个国家,那么您可以直接检索标志数据:

if(!empty($careerpath->location)){
     $flags = \App\Country::where('id', $careerpath->location)->select('flag')->first();   
     dd($flags);
}

【讨论】:

  • 谢谢你的回答,但我已经收到了很好的回答:)
【解决方案2】:
$flags = App\Country::lists('flag', 'id');

那么就可以通过id获取flag了:

$flag = $flags[$careerpath->location];
// or
$flag = $flags->get($careerpath->location);

在你的循环中:

{{ $flags->get($careerpath->location) }}

【讨论】:

  • 嘿,现在我得到 ErrorException in ViewProfileController.php line 645: Trying to get property of non-object ,即 $flag = $flags->get($careerpath->location); 。请再次检查我的控制器,我已编辑它。
  • 循环部分在您的刀片视图中......您正在使用的循环需要获取职业路径位置的标志
猜你喜欢
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-30
  • 2022-01-12
  • 2020-02-27
相关资源
最近更新 更多