Andrew
Andrew

Reputation: 404

How to get flag based on country value?

I have a select-option country list and I need to get from database flag based on these values.

Here is my controller:

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


            }

So, here is the logic :

I have a table for countries called countries.

1.id --- 2.country --- 3.flag

That is my table where I have all my countries.

Here is how I use this country list in my view:

{!! Form::select('location',$country_id ,$user->country->id, ['class' => 'js-example-basic-single']) !!}

where $country_id

$data['country_id'] = \App\Country::lists('country','id');

When I pick something from my select form, like Greece or Italy, in database is saved the id of country from that list.( 20 or 21)

How I get that value in view?Like here:

@foreach($user->career_path as $careerpath)
{{$careerpath->location}}
@endforeach

The problem is I need to pick country and flag values based on id from location field.

So, if my value is 20, I need to get the country name and flag value from table countries based on the value ( id 20 ) from {{$careerpath->location}} . $careerpath->location now returns me id of the country, I need to get, based on that, country name ( from countries.country) and flag value( from countries.flag).

Upvotes: 0

Views: 1219

Answers (2)

Amit Senjaliya
Amit Senjaliya

Reputation: 2945

If you have a country in $careerpath->location then you can direct retrieve flag data:

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

Upvotes: 1

lagbox
lagbox

Reputation: 50561

$flags = App\Country::lists('flag', 'id');

Then you can get the flag by the id:

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

Inside your loop:

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

Upvotes: 0

Related Questions