Your code means nothing to us if we don’t know your data structure. How are the countries and currencies stored in your database? What is $get_countries? What does your Countries model (plural?) represent?
Aug 18, 2022
3
Level 5
Get Data From Collections
hi , i know it's simple issue but i tried every thing i have code to get all currencies
$currencies = $get_countries->all()->pluck('currencies')->toArray();
$get_countries = new Countries();
i want to get it inside blade as select
@foreach($currencies as $currency)
@for($c=0;$c <= count($currencies)-1;$c++)
@if(is_array($currencies[$c]))
@else
<option
{{ old('currency') == $currency ? 'selected' : '' }}
value="{{$currencies[$c]}}"
>{{$currencies[$c]}}</option>
@endif
@endfor
@endforeach
i see all options empty but as the count of options equal the currencies and this collection data
array:267 [▼
0 => array:1 [▶]
1 => array:1 [▶]
2 => array:1 [▶]
3 => array:1 [▶]
4 => array:1 [▶]
5 => array:1 [▶]
6 => array:1 [▶]
7 => array:1 [▶]
8 => array:1 [▶]
9 => array:1 [▶]
10 => array:1 [▶]
11 => []
12 => array:1 [▶]
13 => array:1 [▶]
14 => array:1 [▶]
15 => array:1 [▶]
16 => array:1 [▶]
17 => array:1 [▶]
18 => array:1 [▶]
19 => array:1 [▶]
20 => array:1 [▶]
21 => array:1 [▶]
22 => array:1 [▶]
23 => array:1 [▶]
24 => array:1 [▶]
25 => array:1 [▶]
26 => array:1 [▶]
27 => array:1 [▶]
28 => array:2 [▶]
29 => array:1 [▶]
30 => array:1 [▶]
31 => array:2 [▶]
32 => array:1 [▶]
33 => array:1 [▶]
34 => array:1 [▶]
35 => array:1 [▶]
36 => array:2 [▶]
37 => []
38 => array:1 [▶]
39 => array:1 [▶]
40 => array:1 [▶]
41 => array:1 [▶]
42 => array:3 [▶]
43 => array:2 [▶]
44 => array:1 [▶]
45 => array:1 [▶]
46 => array:1 [▶]
47 => array:1 [▶]
48 => array:1 [▶]
the problem here there's arrays inside arrays inside arrays so i use @if is array to avoid the errors but i still got empty values
80 => array:1 [▼
0 => "GBP"
]
81 => array:1 [▼
0 => "GEL"
]
82 => array:1 [▶]
83 => array:1 [▼
0 => "GHS"
]
84 => array:1 [▼
0 => "GIP"
]
85 => array:1 [▼
0 => "GNF"
]
86 => array:1 [▼
"EUR" => array:2 [▼
"name" => "Euro"
"symbol" => "€"
]
]
87 => array:1 [▶]
Level 51
Firstly, you need to fix the order of the 2 lines
$get_countries = new Countries();
$currencies = $get_countries->all()->pluck('currencies')->toArray();
You don't need to convert the array yourself
$all = $get_countries->all();
Then in your blade template
<select>
@foreach($all as $c)
<option value="{{ $c }}" @selected(old('currency') == $c)>{{ $c }}</option>
@endforeach
</select>
https://github.com/antonioribeiro/countries#get-all-currencies
Please or to participate in this conversation.