How can I get created_at month name on laravel blade? - laravel

I have created_at field. on my database, and I need to get the month name, for example;
...
{{\Carbon\Carbon::now()->monthName}}
...
So how can I do it for $value->created_at?
I tried some solutions but didn't get the month name.

created_at already a Carbon instance, so you can do that by :
{{ $value->created_at->format('F') }}
Or,
{{ \Carbon\Carbon::parse($value->created_at)->format('F') }}
F - A full textual representation of a month (January through
December)
M - A short textual representation of a month (Jan through Dec)
Translate to other language : You can use carbon to format your local language, as for Russian language :
#php
\Carbon\Carbon::setLocale('ru');
#endphp
{{ \Carbon\Carbon::parse($value->created_at)->translatedFormat('F') }}
// output : ноябрь

you can try as created_atalready a Carbon instance, so you can use this
{{ $value->created_at->monthName }}
make sure $value is a instance of Illuminate\Database\Eloquent\Model;
if it is not a instance of laravel model then you can do like this
{{ \Carbon\Carbon::parse($value->created_at)->monthName }}

you can write by
{{ date('F', strtotime($value->created_at)) }}

you can get the name of month by the following code:
{{ date("F", strtotime($value->created_at)) }}
you can get more details from (php website)

Related

Using Eloquent with Spaces in Table Columns

I am using Laravel and have a table that has columns with spaces. I can't change the names of the columns. I am trying to use Eloquent and have tried many things to get this to work but can't. I thought this would be the answer:
{{$provider->'Entity Type Code'}}
Thanks
You can use {} brackets
{{ $provider->{'Entity Type Code'} }}
Or use another variables
$variable_name = 'Entity Type Code';
{{ $provider->$variable_name }}

How to show the curly braces values in blade

Laravel blade view getting values in curly braces like
{"id":4,"patient_id":2,"findings":"Thiese are the
findings","imp":"These are the
impressions","attach":"NA","created_at":"2019-06-14
15:49:37","updated_at":"2019-06-14 15:49:37"}
My question is how to show these values in the blade?
I have tried the following code
#foreach($report as $rrp)
<p>{{ $rrp['cnic'] }}</p>
#endforeach
Above code shows nothing. I tried this too
#foreach($report as $rrp)
<p>{{ $rrp->cnic }}</p>
#endforeach
Not working for me.
You should add more code to your question. What data are you getting, data from database, from request, or something else?
If you data is from database and you saved it to a $reports variable, and let's say you have these columns:
id | patient_id | findings | imp | attach | created_at | updated_at
1 | 2 | some data |some data | some data | some data | some data
And you want to display your text in blade, you could do it like this:
foreach($reports as $report){
<p> {{ $report->id }} </p>
<p> {{ $report->patient_id }} </p>
<p> {{ $report->findings }} </p>
//and so on for every column that you want to display
#endforeach
here is what I got from #Haru comment.
Data in { } is not an array. So, we can show the data like this
<p>ID :{{ $report['id'] }}</p>
Eg: variable is not $reports it is just $report.
It seems you're iterating on the single report.
If the $report contains {"id":4,"patient_id":2,"findings":"Thiese are the findings","imp":"These are the impressions","attach":"NA","created_at":"2019-06-14 15:49:37","updated_at":"2019-06-14 15:49:37"} your loop #foreach($report as $rrp) will iterate the properties (id, patient_id, findings, etc.).
In your case, the answer is:
#foreach($report as $rrp)
<p>{{ $rrp }}</p>
#endforeach
However, if that was an array containing entries like that one, your loop should reflect the existing key. In this example, there's no key like cnic.
Perhaps what you wanted to do was:
#foreach($report as $rrp)
<p>{{ $rrp['id'] }}</p>
<!-- alternatively, if that was an object {{ $rrp->id }} -->
#endforeach

How to display I like it laravel format created_at timestamp

echo `{{$result->created_at}}`
result is 2018-06-23 08:05:14
but I want to echo just date. not time
I mean echo only 2018-06-23 not 2018-06-23 08:05:14
It's a Carbon instance. The recommended option is simply
{{ $result->created_at->toDateString() }}
Carbon docs: https://carbon.nesbot.com/docs/#api-formatting
This is pretty simple, directly escape date to whichever format you want like in the example below
{{ date('Y-m-d', strtotime($result->created_at))}}
You may handle this on the PHP side by just using substr:
echo {{ substr($result->created_at, 0, 10) }}
There are also ways that you could handle the formatting on the MySQL side, e.g. by using DATE_FORMAT:
DATE_FORMAT(created_at, '%Y-%m-%d')
you can use laravel Accessor by put the following method inside your model:
public function getCreatedAtAttribute($date)
{
return $date->format('Y-m-d');
}

Change date format in blade template

I have a date, that prints out as YYYY-MM-DD.
How can I print it out as DD--MM--YEAR, or even better in english: eg 5th May 2018
This is how the date is printed out:
<td>{{$expenses->date}}</td>
You have 3 ways to do this:
1) Using Laravel Model - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
<td>{{$expenses->date->format('j F, Y')}}</td>
2) Using PHP strtotime - Doesn't work well with dates below 1900. You can however fix this by making a few adjustments.
{{ date('j F, Y', strtotime($expenses->date)) }}
3) Using Carbon - Works well with all dates so far
{{ \Carbon\Carbon::parse($expenses->date)->format('j F, Y') }}
This is a duplicate of change the date format in laravel view page you can easily doing this.
<td>{{ date('d-M-y', strtotime($expenses->date)) }}</td>
You can make use of date-mutators
Add date field to your model's dates array.
Expense.php
class Expense extends Model
{
protected $dates = ['date', 'another_date_field'];
//other stuff
}
In view file you can format the date field like,
{{ $expenses->date->format('d-m-Y') }} //01-05-2018
or
{{ $expenses->date->format('l jS \\of F Y h:i:s A') }} //Tuesday 1st of May 2018 01:04:00 PM
Try this:
date('d-m-Y', strtotime($user->from_date));
It will convert date into d-m-Y format.
Try this in blade. It will show nicely with <sup> tags. Know this is an old thread but adding since it took lots of time for me to find this and wishing it'll be easier to future users.
{!! htmlspecialchars_decode(date('j<\s\up>S</\s\up> F Y', strtotime('21-05-2020'))) !!}
In Laravel you can add a function inside app/Helper/helper.php like
function formatDate($date = '', $format = 'Y-m-d'){
if($date == '' || $date == null)
return;
return date($format,strtotime($date));
}
And call this function on any controller like this
$expenses['date'] = formatDate($date,'d-m-Y');
And in Blade you can write directly
<td>{{$expenses->date}}</td>
Hope it helps!
The Carbon library is really cool with date formatting in laravel. The topic of String Formatting will be very useful to you.
Use of carbon in blade php can be handled by this format:
{{ \Carbon\Carbon::now()->toDateString() }}

Laravel query builder using hasMany output

This is my syntax in blade file :
{{ $product->filters->where("filter_id","10") }}
And my output for this looks like :
{"3":{"id":7153,"product_id":"1","filter_id":"10","data":"Kajaria","created_at":null,"updated_at":null}}
And I want to extract only a particular data i.e. Kajaria.
I'm getting confused in writing syntax.
I had already tried these syntaxes :
{{ $product->filters->where("filter_id","10")->data }}
{{ $product->filters->where("filter_id","10")['data'] }}
Try something like this:
{{ $product->filters->where("filter_id","10")->first()->data }}
The thing is $product is a collection and you need to get property of just one object, so first() method returns first object from a collection which you can use.
What I understand is that you want to get a filter for a product. So, try this.
Product::with('filters')->where('filter_id', 10)->first()->data;

Resources