how to read Currencies Exchange Rates right? - php

Does anyone know a way to read the currency exchange rates right in laravel?
We develop web exchange currency so I have a problem when I put right rates but get a different results.
Example: Exchange between USD TO EGP : 1 USD = 15.70 EGP So i need to Put the Currency USD TO get 15.70 EGP but if i put this will get Rates - 1 USD = 4 EGP not 15.70
Follow the pictures to understand
1-
2-
3-
4-
So my question is:
Does anybody know a way to solve these rates?
Home Blade
<form class="exchange-form" method="POST" action="{{ route('user.exchange') }}">
#csrf
<div class="form-group">
<label for="send">#lang('You Send')</label>
<input type="text" name="send_amount" id="send_val" placeholder="#lang('Send')" required
onkeyup="this.value = this.value.replace (/^\.|[^\d\.]/g, '')">
<select class="select-bar" name="send" id="send">
<option value="">#lang('Select Currency')</option>
#foreach ($currencys_sell as $currency)
<option value="{{ $currency->id }}"
data-min_max="{{ filterCollection($currency, 'rate', 'sell_at', 'buy_at', 'fixed_charge', 'percent_charge', 'reserve', 'min_exchange', 'max_exchange', 'cur_sym', 'payment_type_sell') }}">
{{ $currency->name }} {{$currency->cur_sym}}
</option>
#endforeach
</select>
<div class="exchange-limit exchange-buy d-none">
<div class="item">
<span class="subtitle">#lang('Min')</span>
<span class="amount min-amount"></span>
</div>
<div class="item">
<span class="subtitle">#lang('Max')</span>
<span class="amount max-amount"></span>
</div>
</div>
</div>
<div class="form-group receiveData">
<label for="receive">#lang('You Get')</label>
<input type="text" name="receive_amount" id="receive_val" min="0" placeholder="#lang('Receive')"
readonly>
<select class="select-bar" name="receive" id="receive">
<option value="" class="wrap">#lang('Select Currency')</option>
#foreach ($currencys_buy as $currency)
<option value="{{ $currency->id }}"
data-min_max="{{ filterCollection($currency, 'cur_sym', 'rate', 'sell_at', 'buy_at', 'fixed_charge', 'percent_charge', 'reserve', 'min_exchange', 'max_exchange', 'payment_type_sell') }}">
{{ $currency->name }} {{$currency->cur_sym}}
</option>
#endforeach
</select>
<div class="exchange-limit reserve-section d-none">
<div class="item reserve">
<span class="subtitle">#lang('Reserve')</span>
<span class="amount"></span>
</div>
<div class="item reserve">
<span class="subtitle">#lang('Rate')</span>
<span class="amount conversion"></span>
</div>
</div>
</div>
Home Controller
public function exchange(Request $request)
{
session()->forget('Track');
$receive = Currency::find($request->receive);
$send = Currency::find($request->send);
if ($receive == null) {
$notify[] = ['error', 'Select any method that we send u the money'];
return back()->withNotify($notify);
}
if ($send == null) {
$notify[] = ['error', 'Select any method that we get money'];
return back()->withNotify($notify);
}
$field = json_decode($receive->user_input);
$validate_array = [
'send' => 'required|numeric',
'send_amount' => 'required|numeric|gt:0',
'receive' => 'required|numeric',
'receive_amount' => 'required|numeric|gt:0',
];
foreach ($field as $value) {
if (strtolower($value->type) === 'email') {
$validate_array[$value->field_name] = "sometimes|{$value->validation}|email";
continue;
}
$validate_array[$value->field_name] = "sometimes|{$value->validation}";
}
$this->validate($request, $validate_array);
// new Calculation for covert amount
$percentCharge = ($request->send_amount * $send->percent_charge) / 100;
$totalCharge = $percentCharge + $send->fixed_charge;
$totalSendAmount = $request->send_amount - $totalCharge;
$sendAmountConvertInBaseCurrency = $totalSendAmount * $send->buy_at;
$userReceiveAmount = $sendAmountConvertInBaseCurrency / $receive->sell_at;

Related

Filtering table results with drop down menu in Laravel

Created a drop down menu that is supposed to work as a filter. I am filtering with enum values so not sure how to make it work. The view + route are working fine. When the button is pressed the URL changes and adds the selected enum value /dropdown?filter_status=inactive. But the results are not filtered.
View:
<div class="container d-flex justify-content-start">
<div class="row">
<div class="col-md-12">
<form action="{{ route('dropdownView') }}" method="GET">
<div class="form-group">
<div>
<span>
<select class="mb-2" name="filter_status" >
<option value="Status" disabled selected>Status</option>
#foreach ($enumoption as $status)
<option value="{{ $status }}">{{$status}}</option>
#endforeach
</select>
<button type="submit" class="btn btn-primary btn-sm ms-1 mb-1">Filter</button>
</span>
</div>
</div>
</form>
</div>
</div>
</div>
Route:
Route::get("/dropdown", [CompetitionController::class, 'index'])->name('dropdownView');
Controller(getPossibleEnumValues is the function I am using in my model to get the enum values)
$enumoption = Competition::getPossibleEnumValues('competitions','status');
EDIT:
public static function getPossibleEnumValues($table, $column) {
$type = DB::select(DB::raw("SHOW COLUMNS FROM $table WHERE Field = '{$column}'"))[0]->Type ;
preg_match('/^enum\((.*)\)$/', $type, $matches);
$enum = array();
foreach( explode(',', $matches[1]) as $value )
{
$v = trim( $value, "'" );
$enum = Arr::add($enum, $v, $v);
}
return $enum;
}

How do I update records in a loop in Laravel 8

I am developing a student portal in Laravel 8. I would love to update the class subjects.
Here is my controller
public function UpdateAssignSubject(Request $request, $class_id)
{
if ($request->subject_id == null) {
$notification = [
'message' => 'Sorry You did not Select any Subject',
'alert-type' => 'error'
];
return redirect()->route('assign.subject.edit', $class_id)->with($notification);
} else {
$countClass = count($request->subject_id);
AssignSubject::where('class_id', $class_id)->delete();
for ($i = 0; $i < $countClass; $i++) {
$assign_subject = new AssignSubject();
$assign_subject->class_id = $request->class_id;
$assign_subject->subject_id = $request->subject_id[$i];
$assign_subject->full_mark = $request->full_mark[$i];
$assign_subject->pass_mark = $request->pass_mark[$i];
$assign_subject->subjective_mark = $request->subjective_mark[$i];
$assign_subject->save();
} // End For Loop
}// end Else
$notification = [
'message' => 'Assigned Subject Updated Successfully',
'alert-type' => 'success'
];
}
This very piece of code AssignSubject::where('class_id', $class_id)->delete(); is giving me issues since I am using the AssignSubject as a pivot table thus deleting the Id's produces errors in the long run.
Here is my view
#foreach($subjects as $subject)
<option value="{{ $subject->id }}" {{ ($edit->subject_id == $subject->id)? "selected": ""
}}>{{ $subject->name }}</option>
#endforeach
<div class="form-group">
<h5 style="color:black">Full Mark <span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="full_mark[]" value="{{ $edit->full_mark }}" class="form-control"
style="background-color: rgb(176, 172, 216);color:black" >
</div>
<div class="col-md-2">
<div class="form-group">
<h5 style="color:black">Pass Mark <span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="pass_mark[]" value="{{ $edit->pass_mark }}" class="form-control"
style="background-color: rgb(176, 172, 216);color:black">
</div>
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<h5 style="color:black">Subjective Mark <span class="text-danger">*</span></h5>
<div class="controls">
<input type="text" name="subjective_mark[]" value="{{ $edit->subjective_mark }}" class="form-
control" style="background-color: rgb(176, 172, 216);color:black">
</div>
</div>
</div>
Please how can I update the record without deleting first. Any help would be greatly appreciated.
Instead of deleting all entities, you can simply update them if they do exist.
In case they do not, you need to create a new one, like you did in your for loop.
I adjusted your code, to check if a subject already exists, if it does it updates it, otherwise a new one is created
$countClass = count($request->subject_id);
// remove this line
// AssignSubject::where('class_id', $class_id)->delete();
for ($i = 0; $i < $countClass; $i++) {
// a class can only have each subject 1 time, so you can filter for it
$entity = AssignSubject::where('class_id', $class_id)
->where('subject_id', $request->subject_id[$i])
->first();
// if entry does not exist, ->first() will return null, in this case, create a new entity
$assign_subject = $entity ?? new AssignSubject();
// update columns
$assign_subject->class_id = $request->class_id;
$assign_subject->subject_id = $request->subject_id[$i];
$assign_subject->full_mark = $request->full_mark[$i];
$assign_subject->pass_mark = $request->pass_mark[$i];
$assign_subject->subjective_mark = $request->subjective_mark[$i];
// save changes
$assign_subject->save();
}

How to remove unwanted values from arrayed input?

I just wanted to ask how can we avoid this kind of output from arrayed input. Every time I update it, these symbols ["\"[ keeps on multiplying. I'll show you the problem and the code below.
Thank you your future answers.
Route::resource('setups','SetupController');
public function index()
{
$data = DB::table('setups')->first();
if (!empty($data)) {
$socials = explode(',',$data -> social);
}else{
$socials = [];
}
return view ('adminpanel.setup.index',['data' => $data,'socials' => $socials]);
}
index.blade.php
<form action="{{ route('setups.edit',$data->id) }}">
<div class="row">
<div class="col-md-12" id="socialGroup">
#foreach($socials as $social)
<div class="form-group socialField">
<label class="bmd-label-floating">Social Links</label>
<input type="text" name="social[]" value="{{$social}}" class="form-control" disabled>
<i class="fa fa-plus"></i>
</div>
#endforeach
<div class="alert alert-danger" id="socialError">
<p><strong>Sorry! </strong>You've reached the max number for social links form.</p>
</div>
</div>
</div>
<form>
.
public function edit($id)
{
$data = DB::table('setups')->first();
$setup = DB::table('setups')->where('id', $id)->first();
if (!empty($data)) {
$socials = explode(',',$data -> social);
}else{
$socials = [];
}
if($setup){
return view ('adminpanel.setup.edit',['data' => $data,'socials' => $socials]);
}else{
return redirect('setups');
}
}
.
edit.blade.php
<form method="POST" action="{{ route('setups.update', $data->id) }}">
<div class="row">
<div class="col-md-12" id="socialGroup">
#foreach($socials as $social)
<div class="form-group socialField">
<label class="bmd-label-floating">Social Links</label>
<input type="text" name="social[]" value="{{ $social }}" class="form-control">
<i class="fa fa-plus"></i>
</div>
#endforeach
<div class="alert alert-danger" id="socialError">
<p><strong>Sorry! </strong>You've reached the max number for social links form.</p>
</div>
</div>
</div>
<form>
.
public function update(Request $request, Setup $setup)
{
$data = Input::except('_token', 'submit', '_method');
$tbl = decrypt($data['tbl']);
unset ($data['tbl']);
$data['updated_at'] = date('Y-m-d H:i:s');
DB::table($tbl)->where(key($data), reset($data))->update($data);
session::flash('message','Setup updated successfully!!!');
return redirect()->route('setups.index');
}
Solved! I just added this code in my SetupController#update to illuminate those unwanted divider or separator(whatever) before
sending to database.
if (Input::has('social')) {
$data['social'] = implode(',',$data['social']);
}
laravel escaped data by default. It was not giving any error,whenever
you retrieve data from database to throw in your blade view.Database
data escaping is good practice.
As you showed your data,there is some unwanted data.Before you attempt to save your data,you may trim($yourString) to remove unwanted white-space from start & end of a string.
And You must not let blank or empty string to view in your blade. So, you might use blank($var) to check whether it is blank or not?
<form method="POST" action="{{ route('setups.update', $data->id) }}">
<div class="row">
<div class="col-md-12" id="socialGroup">
#foreach($socials as $social)
#if(!blank($social))
<div class="form-group socialField">
<label class="bmd-label-floating">Social Links</label>
<input type="text" name="social[]" value="{{ $social }}" class="form-control">
<i class="fa fa-plus"></i>
</div>
#endif
#endforeach
<div class="alert alert-danger" id="socialError">
<p><strong>Sorry! </strong>You've reached the max number for social links form.</p>
</div>
</div>
</div>
Solved! I just added this code in my SetupController#update
if (Input::has('social')) {
$data['social'] = implode(',',$data['social']);
}

Search using dropdown options with additional filtering - Laravel 5.7

I want to add the option when 'recommendation' or 'source' is selected from dropdown, to get all Candidates which have in one of those two columns something other than null or '0', and sort them using 'created_at', from-to date.
This is my view:
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12">
<form class="search-form search-form-basic" action="/candidates/index" method="post">
{{ csrf_field() }}
<div class="form-row">
<div class="col-md-4 form-group">
<label for="search_email">First name:</label>
<input type="text" name="search_first_name" id="search_first_name" class="form-control" #if(isset(Session::get('inputs')['search_first_name'])) value="{{ Session::get('inputs')['search_first_name'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_last_name">Last name:</label>
<input type="text" name="search_last_name" id="search_last_name" class="form-control" #if(isset(Session::get('inputs')['search_last_name'])) value="{{ Session::get('inputs')['search_last_name'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_round_number">Round Number:</label>
<input type="text" name="search_round_number" id="search_round_number" class="form-control" placeholder="" #if(isset(Session::get('inputs')['search_round_number'])) value="{{ Session::get('inputs')['search_round_number'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_location">location:</label>
<input type="text" name="search_location" id="search_location" class="form-control"
#if(isset(Session::get('inputs')['search_location'])) value="{{ Session::get('inputs')['search_location'] }}" #endif>
</div>
<select name="options" class="col-md-4 form-group">
<option value="">--- Select From ---</option>
<option value="birth_date">Birth Date</option>
<option value="CV_grade_date">CV Grade Date</option>
<option value="source">Source</option>
<option value="recommendation">Recommended</option>
<option value="created_at">Applied</option>
</select>
<div class="col-md-4 form-group">
<label for="search_from_date">From:</label>
<input type="date" name="search_from_date" id="search_from_date" class="form-control"
#if(isset(Session::get('inputs')['search_from_date'])) value="{{ Session::get('inputs')['search_from_date'] }}" #endif>
</div>
<div class="col-md-4 form-group">
<label for="search_to_date">To:</label>
<input type="date" name="search_to_date" id="search_to_date" class="form-control"
#if(isset(Session::get('inputs')['search_to_date'])) value="{{ Session::get('inputs')['search_to_date'] }}" #endif>
</div>
</div>
<div class="form-row">
<div class="col-md-12 col-lg-12">
<button type="submit" class="btn btn-custom"><i class="fa fa-search" aria-hidden="true"></i>Search</button>
<i class="fa fa-times-circle" aria-hidden="true"></i>Clear
</div>
</div>
</form>
</div>
This is my Controller, where i tried to implement adding a new variable with value of either 'recommendation' or 'source', assign that value to a new variable, and then drop the $options variable, so I skip
->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) {
return $query->whereBetween($options, array($search_from_date, $search_to_date));
Here is my code:
public function index(Request $request) {
if($request->isMethod('post')){
$search_first_name = $request->search_first_name;
$search_last_name = $request->search_last_name;
$search_email = $request->search_email;
$search_round_number = $request->search_round_number;
$search_location = $request->search_location;
$options = Input::get('dropdown');
if($options == 'recommendation' || 'source') {
$recOrSource = Input::get('options');
unset($options);
}
$search_from_date = $request->search_from_date;
$search_to_date = $request->search_to_date;
$candidate = DB::table('candidates')
->when($search_first_name, function ($query) use ($search_first_name) {
return $query->where('first_name', 'like', '%' . $search_first_name . '%');
})
->when($search_last_name, function ($query) use ($search_last_name) {
return $query->where('last_name', 'like', '%' . $search_last_name . '%');
})
->when($search_email, function ($query) use ($search_email) {
return $query->where('email', $search_email);
})
->when($search_round_number, function ($query) use ($search_round_number) {
return $query->where('round_number', $search_round_number);
})
->when($search_location, function ($query) use ($search_location) {
return $query->where('location', $search_location);
})
->when($options, function ($query) use ($options,$search_from_date,$search_to_date ) {
return $query->whereBetween($options, array($search_from_date, $search_to_date));
})
->when($recOrSource, function ($query) use ($recOrSource,$search_from_date,$search_to_date ) {
return $query->where(!empty($recOrSource))->whereBetween('created_at', array($search_from_date, $search_to_date));
})
->orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();
Session::flash('inputs', [
'search_first_name' => $search_first_name,
'search_last_name' => $search_last_name,
'search_email' => $search_email,
'search_round_number' => $search_round_number,
'search_from_date' => $search_from_date,
'search_to_date' => $search_to_date,
'search_location' => $search_location
]);
}else{
Session::forget('inputs');
$candidate = Candidate::orderBy('first_name', 'asc')
->orderBy('last_name', 'asc')
->get();
}
return view('/candidates/index', [
'candidate' => $candidate
]);
}
The error I get is "Undefined variable: options", However I didn't find a way to resolve this.
And I did see similar questions but nothing was working for me.
1) In your view the <select> name attribute is options, but in your controller you are retrieving the value of dropdown $options = Input::get('dropdown');. It should be:
$options = Input::get('options');
2) If you want check if the selected option is recommendation or source, your if statement should be:
if ($options == 'recommendation' || $options == 'source')
3) Inside your if statement you are unsetting $options variable, but lines below you have when($option,.., then you should change unset($options); to:
$options = null;
otherwise it will throw "Undefined variable: options" again.
4) Also, in your controller you using ->when($recOrSource,.., but the variable $recOrSource is declared inside your if statement, so If you don't select "recommended" or "source", you will get "Undefined variable: recOrSource". To fix that you should declare $recOrSource i.e. outside your if statement.
$recOrSource = null;
if ($options == 'recommendation' || $options == 'source') {
Wrapping up, the changes you should do in your controller:
...
$options = Input::get('options');
$recOrSource = null;
if($options == 'recommendation' || $options == 'source') {
$recOrSource = Input::get('options');
$options = null;
}
...

Hide an element when a toggle is checked in PHP/Laravel

I am trying to hide an element on my page when I click a toggle and unhide it when I click the toggle again. Here is my HTML code:
<div class="form-group settings-row">
{{ Form::label('notificationSettings', 'Notification Settings', array('class' => 'col-md-2 control-label')) }}
<div class="col-md-10">
<table style="border-width:0; margin-top:4px;">
<tr>
<td>
Notifications Enabled
</td>
<td>
<div class="onoffswitch" style="margin:4px 0 0 7px;">
<input type="checkbox" #if($notificationsEnabled == true)checked #endif name="notifications_toggle" class="onoffswitch-checkbox" id="notifications_toggle">
<label class="onoffswitch-label" for="notifications_toggle">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>
</td>
<td></td>
</tr>
</table>
<div class="col-md-10">
#foreach($notificationMessageRadios as $radio)
#if(isset($radio['text']))
<div class="row" style="margin-bottom:1em;">
<input type='radio' name='notificationMessage' id="notificationmessage-other" value="" #if($radio['selected']) checked #endif> {{$radio['message']}}
</div>
<input type="text" id="notificationMessageCustom" placeholder="Pick your own!" #if($radio['selected'])value="{{ $settingsForMessages->notification_message }}" #else value="Pick your own!" #endif name="notificationMessageCustom" class="form-control" #if(!$radio['selected'])style="display:none"#endif>
#else
<div class="row">
<input type='radio' name='notificationMessage' value="{{ $radio['message']}}" #if($radio['selected']) checked #endif> {{ $radio['message'] }}
</div>
#endif
#endforeach
</div>
</div>
</div>
Breakdown:
So when I click the onoffswitch to be on, I want the radios to appear. When I click the onoffswitch to be off, I want the radios to disappear.
I am being thrown into this project, so this code already existed and I am being tasked with editing some features. Any kind of help would be greatly appreciated.
As a bonus, here is the controller side:
$notificationsEnabled = ( $settings['notificationsEnabled'] == 1);
// Cast notification messages to a variable
$notificationMessageMsgs = \Config::get('customer_messages.notificationMessageArray');
$numNotificationMessageMsgs = count($notificationMessageMsgs);
$notificationMessageSelected = false;
// Determine which email subject radio button is checked
foreach($notificationMessageMsgs as $index => $msg) {
$isSelected = false;
if ($settingsForMessages->notification_message == null && $index == 0) {
$notificationMessageSelected = true;
$isSelected = true;
}
if($msg['message'] === $settingsForMessages->notification_message){
$isSelected = true;
$notificationMessageSelected = true;
}
if($index == $numNotificationMessageMsgs - 1 && !$notificationMessageSelected){
$isSelected = true;
}
$notificationMessageRadios[] = array_merge(['selected' => $isSelected], $msg);
}
I am NOT looking for hand holding. I am looking for guidance, that is all.
Maybe try out jQuery?
Try out jQuery Toggle : https://www.w3schools.com/jquery/eff_toggle.asp
$("button").click(function(){
$("p").toggle();
});
This is the code copies from the link . just target your "Class" , "ID" , "element" in the top row $("button") that needs to be clicked and than target the element that needs to be shown/hidden $("p")

Categories