Laravel 4 echo "Orignal" float value in Blade - laravel-4

I save float number from input to MySQL.
I put "3.2" and "486.2" then save, in MySQL store as "3.2", "486.2", same as input and this field have type float.
But then i call this number from MySQL to Blade in Laravel like:
{{ $Item->price }} and {{ $Item->totalPay }}
the page show me: 3.2 => 3.2000000476837
and 486.2 => 486.20001220703
What is wrong i am was do?
Does this cause by Blade Engine or something?
I try to round() but this seem cannot.

This is a problem with float, specifically with trying to store decimals as a float since they cannot properly be represented. You should try using a decimal field in your database since you are stocking currency data. Or even better would be to use an integer field, saving your numbers as 320 and 48620, and then doing all calculations as such, and then dividing the value by 100, or formatting the number before you output it.

{{ $Item->price }} and {{ $Item->totalPay }}
{{ number_format($Item->price,5) }} and {{ number_format($Item->totalPay,5) }}
this will show
3.2 => 3.20000 and 486.2 => 486.20001

Related

Hugo data files from dynamic parameter

I'm developing a big hugo template. I try to simplfy the problem, so I have two datafile:
PROMO_00_1.yaml
PROMO_00_2.yaml
that are phisically stored in this directory:
themes/data/hp/
So, in the site config the user will decide which of this data file will be used simply indicate it in a param (HpElement).
In the template I call the partial in this way:
{{ partial "multiplepages/homepage/promos/00_promo_singleslide_text_video" (dict "context" . "data" $.Site.Params.HpElement) }}
In a partial I write:
{{ $data_partial := (printf "$.Site.Data.homepage.%s" .data)}}
{{ $data_partial}}
and the Hugo output is on the website:
$.Site.Data.homepage.PROMO_00_1
What I need is to access the single variable inside the .yaml file but the user MUST can decide which YAML file have to use. How can I achieve that?
Thanks
I just finished up a similar use case where I needed to select a YAML based on a date stamp. The index function is key to this operation.
https://gohugo.io/functions/index-function/
Here is a simplified version of my situation:
{{ $date := now.Format "s20060102"}}
{{ $data := (index .Site.Data.schedule $date) }}
{{ with $data }}
<h1>.longdate</h1>
{{ range .times }}
<h2>{{ .name }} - {{ .location }}
{{ end}
{{ end}
The example in the Hugo documentation uses an example where the data file is selected based on an attribute in the front matter.

What does it mean {{ trans(core.text) }} in laravel?

I am new in Laravel and I am working on inbuilt project I have got {{ trans(core.text) }} after browse on internet I find that trans is a helper but I am not getting any file. So, Any one explain what does the line meaning {{ trans(core.text) }}.
Thank You
The trans function translates the given translation key using your localization files:
echo trans('messages.welcome');
If the specified translation key does not exist, the trans function will return the given key.
Link : https://laravel.com/docs/master/helpers

Laravel 5.x and Columns name with blank space

In my blade template I have a problem with a DB column with a blank space.
{{ $data->first name }} doesn't work.
Is there any alternative solution?
P.s. I can't modify my column name.
You can use the following syntax to access the column:
{{ $data->{'first name'} }}
or
{{ $data['first name'] }}

Format phone number in Laravel Blade

Is there a simple way to format a phone string for display in a Laravel Blade template?
I am trying to take '612345678'
and insert it as {{ $user{'phone'] }} and have it display as 612 345 678
Feel free to use the following package:
https://github.com/Propaganistas/Laravel-Phone
and use it for example:
{{ phone('612345678'); }}
{{ phone($user['phone'], 'US'); }}
add the function in the model
public function phoneNumber() {
// add logic to correctly format number here
// a more robust ways would be to use a regular expression
return "(".substr($data, 0, 3).") ".substr($data, 3, 3)." ".substr($data,6);
}
and display it into your blade as
{{ $user->phoneNumber() }}
This will give you (###) ###-####
preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $simple->phone)
You can try this:
$format = chunk_split($user['phone'], 3, ' ');
You will get your phone number as "612 345 678 " with a space trailing behind.
To remove the last space trailing behind, you can use rtrim() function.
rtrim($format, ' ');
Hope this works.

Laravel 4 Carbon Format with if statement

Does anyone know how I could get this to work.
My database has some null dates, so I would like to return as empty space.
I currently have this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
and from what I read about the blade template is that you can use "or 'message'" after it.
I did this.
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') or '' }}
in hopes to just show empty space but I get a "1" instead.
Anyone know why and/or how to get around this?
Well, you should rather do it using simple condition:
#if ($chauffeur->roadTestCert !== null)
{{ Carbon::parse($chauffeur->roadTestCert)->format('m/d/Y') }}
#endif

Resources