Format phone number in Laravel Blade - laravel

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.

Related

Changing a html class after the first item in a Hugo loop

So I am making a review carousel using Bootstrap and Hugo, I've got code that breaks down into this:
{{ range seq 1 3 (len site.Data.reviews) }}
...
{{ range seq . (add . 2) }}
{{ with (index site.Data.reviews (string .)) }}
{{ .des }}
{{ end }}
{{ end }}
...
{{ end }}
So there's two loops, one to make the slides for the carousel, and the other to fill the slides with data files. The issue is I need to delete the active class and adjust the data-bs-interval input on the next few slides I thought about making an if statement but I'm not sure how to replace the first div with one that doesn't have the active class after that in whats generated.
I don't know if this is the best solution to it, instead of editing the loop I wrote a bit of javascript:
var addActive = document.getElementById('carouselExampleDark').getElementsByClassName('carousel-item')[0];
addActive.classList.add("active");
That works for my use case so I'll leave it at that.

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 4 echo "Orignal" float value in Blade

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

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

How to concatenate strings in twig

Anyone knows how to concatenate strings in twig? I want to do something like:
{{ concat('http://', app.request.host) }}
This should work fine:
{{ 'http://' ~ app.request.host }}
To add a filter - like 'trans' - in the same tag use
{{ ('http://' ~ app.request.host) | trans }}
As Adam Elsodaney points out, you can also use string interpolation, this does require double quoted strings:
{{ "http://#{app.request.host}" }}
Also a little known feature in Twig is string interpolation:
{{ "http://#{app.request.host}" }}
The operator you are looking for is Tilde (~), like Alessandro said, and here it is in the documentation:
~: Converts all operands into strings and concatenates them. {{ "Hello
" ~ name ~ "!" }} would return (assuming name is 'John') Hello John!. – http://twig.sensiolabs.org/doc/templates.html#other-operators
And here is an example somewhere else in the docs:
{% set greeting = 'Hello' %}
{% set name = 'Fabien' %}
{{ greeting ~ name|lower }} {# Hello fabien #}
{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }} {# hello fabien #}
In this case, where you want to output plain text and a variable, you could do it like this:
http://{{ app.request.host }}
If you want to concatenate some variables, alessandro1997's solution would be much better.
{{ ['foo', 'bar'|capitalize]|join }}
As you can see this works with filters and functions without needing to use set on a seperate line.
Whenever you need to use a filter with a concatenated string (or a basic math operation) you should wrap it with ()'s. Eg.:
{{ ('http://' ~ app.request.host) | url_encode }}
You can use ~ like {{ foo ~ 'inline string' ~ bar.fieldName }}
But you can also create your own concat function to use it like in your question:
{{ concat('http://', app.request.host) }}:
In src/AppBundle/Twig/AppExtension.php
<?php
namespace AppBundle\Twig;
class AppExtension extends \Twig_Extension
{
/**
* {#inheritdoc}
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('concat', [$this, 'concat'], ['is_safe' => ['html']]),
];
}
public function concat()
{
return implode('', func_get_args())
}
/**
* {#inheritdoc}
*/
public function getName()
{
return 'app_extension';
}
}
In app/config/services.yml:
services:
app.twig_extension:
class: AppBundle\Twig\AppExtension
public: false
tags:
- { name: twig.extension }
In Symfony you can use this for protocol and host:
{{ app.request.schemeAndHttpHost }}
Though #alessandro1997 gave a perfect answer about concatenation.
Quick Answer (TL;DR)
Twig string concatenation may also be done with the format() filter
Detailed Answer
Context
Twig 2.x
String building and concatenation
Problem
Scenario: DeveloperGailSim wishes to do string concatenation in Twig
Other answers in this thread already address the concat operator
This answer focuses on the format filter which is more expressive
Solution
Alternative approach is to use the format filter
The format filter works like the sprintf function in other programming languages
The format filter may be less cumbersome than the ~ operator for more complex strings
Example00
example00 string concat bare
{{ "%s%s%s!"|format('alpha','bravo','charlie') }}
--- result --
alphabravocharlie!
Example01
example01 string concat with intervening text
{{ "The %s in %s falls mainly on the %s!"|format('alpha','bravo','charlie') }}
--- result --
The alpha in bravo falls mainly on the charlie!
Example02
example02 string concat with numeric formatting
follows the same syntax as sprintf in other languages
{{ "The %04d in %04d falls mainly on the %s!"|format(2,3,'tree') }}
--- result --
The 0002 in 0003 falls mainly on the tree!
See also
http://twig.sensiolabs.org/doc/2.x/filters/format.html
https://stackoverflow.com/tags/printf/info
To mix strings, variables and translations I simply do the following:
{% set add_link = '
<a class="btn btn-xs btn-icon-only"
title="' ~ 'string.to_be_translated'|trans ~ '"
href="' ~ path('acme_myBundle_link',{'link':link.id}) ~ '">
</a>
' %}
Despite everything being mixed up, it works like a charm.
The "{{ ... }}"-delimiter can also be used within strings:
"http://{{ app.request.host }}"

Resources