1

I'm working on displaying currency list from table in dropdown in laravel blade. In the dropdown option i want to set first value currency as set in csv file. but dropdown records are from table.

how do i set first value in dropdown as set in uploaded csv file.

Thanks for your help.

following are my code.

my.blade.php

storing currency from csv in variable = $array['currency']

<td class="currency"style="width: 10%;">  
    {{ Form::select('currency',$currencies , "",['class'=>"form-control   select-currency",'data-plugin'=>"select2",
    ]) }}
 </td>

MyModel.php

$currencies = Currency::all()->pluck('code', 'id');
 $questions_table = \View::make('confirmation.form_questionnaire',
['questions' => $record['questions'], 'response_type' =>$record['response_type'], 'currencies' => $currencies]);

dropdown

  • You mean the first part of the drop-down need to come from CSV and secound part is from the Currency ? – DsRaj Jun 22 '18 at 11:02
  • thanks for your reply, not exactly in dropdown all the data (currency list) will be displayed from TABLE values, but the first value will be displayed as set in CSV file. – Rohit Dube Jun 22 '18 at 11:04
  • eg. in csv file in currency = USD then in dropdown = USD value will be display first then all other currency value. – Rohit Dube Jun 22 '18 at 11:08
  • the value USD is also in the table? Or both table and csv have different values? – DsRaj Jun 22 '18 at 11:13
  • yes USD is also in table, no the table and csv have same values. – Rohit Dube Jun 22 '18 at 11:14

1 Answers1

0

Updated

Order by CSV filed Currency code

$csvFiled = 'USD';
$currencies = Currency::orderByRaw("FIELD(code , '$csvFiled') DESC")->pluck('code', 'id');

OR

Very first get the list in the array

$currencies = Currency::pluck('code', 'id')->toArray();

then find the CSV record and set as the first index

$csvValuekey = array_search('USD', $currencies); // $key = 2;
unset($users[$csvValuekey]); // Remove from main array 
$fOption = [$csvValuekey => 'USD'];
$finallList = array_merge($fOption,$currencies); // Merge and set on top 

Now add the $finallList in form select

DsRaj
  • 2,288
  • 1
  • 16
  • 26
  • the records will come from table, and the currency set in csv file will match with table record and set to first – Rohit Dube Jun 22 '18 at 11:47
  • Yes I understand, $csvFiled set your csv field at here and then run the codes otherwise set the $csvFiled as as static let say as 'USD' then print the result of $currencies – DsRaj Jun 22 '18 at 11:51
  • {{ $selected_currency = \App\Models\Currency::orderByRaw("FIELD('code' , $array['currency']) DESC")->pluck('code', 'id')}} – Rohit Dube Jun 22 '18 at 12:01
  • echo the value of $array['currency'] and paste in comment OR add $csvFiled = $array['currency']; rest is same – DsRaj Jun 22 '18 at 12:19
  • ohh sorry, my mistake got it, i was passing with string $array['currency'] that's why it was giving me an error – Rohit Dube Jun 22 '18 at 12:21
  • I will suggest you to print it like this: echo '
    ';print_r($currencies->toArray());exit;
    – DsRaj Jun 22 '18 at 12:25
  • in this $csvValuekey = array_search('USD', $users), $users is empty array object or what? – Rohit Dube Jun 22 '18 at 12:39
  • I think you didn't check the answer you need to go with first option – DsRaj Jun 22 '18 at 12:46
  • Ohhh and the users is currencies that is by mistake: Updated – DsRaj Jun 22 '18 at 12:47
  • {{ Form::select('currency',\App\Models\Currency::orderByRaw("FIELD(code , '$val') DESC")->pluck('code', 'id'), "",['class'=>"form-control select-currency", 'data-plugin'=>"select2"]) }} – Rohit Dube Jun 22 '18 at 13:42
  • above lines work for me, @DsRaj Thanks for your time and help – Rohit Dube Jun 22 '18 at 13:42
  • you are running query on in blade file? – DsRaj Jun 22 '18 at 13:45
  • will there be problem? running queries in blade file. since i'm new to laravel – Rohit Dube Jun 23 '18 at 15:09
  • This is not good way, add the query in model and get the data from there via controller and pass the variable/array/object to view and use it – DsRaj Jun 25 '18 at 04:47
  • my.blade.php {{ Form::select('currency', $currencies, $array['currency'], ['class'=>"form-control select-currency", 'data-plugin'=>"select2"]) }} – Rohit Dube Jun 26 '18 at 12:26
  • MyModel.php $currencies = Currency::pluck('code', 'id')->toarray(); return view('my.blade.php', $currencies) – Rohit Dube Jun 26 '18 at 12:27
  • Add the code in the model in a function call the function from controller and get value – DsRaj Jun 26 '18 at 13:33