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
Related
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)
Below is how i view count of on my dashboard for all devices i store, where 'devices' are all devices am storing in my database. l want to view a count using a specific column name in a row. how do i do that!, any help?
#inject('devices', 'App\Device')
<div class="number"><strong>{{ $devices->count() }}</strong></div>
Are you looking for this?
$devices->where('column', 'matches')->count();
Or something like this maybe..
$devices->select('yourColumn', DB::raw('count(*) as total'))->groupBy('yourColumn')->get()
So i found out that i had to do something like this:
{{ $devices->where('Type_ID',
'Laptop')->count() }}
from: <div class="number"><strong>{{ $devices->count() }}</strong></div>
to something like this and it worked perfectly:
<div class="number"><strong>{{ $devices->where('Type_ID', 'Laptop')->count() }}</strong></div>
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() }}
Controller
$new_products = Product::with('images')->orderBy('created_at', 'desc')->take(10)->get();
return view('site/home', compact('new_products'));
View
{{ $new_product->images[0]->image_name }}
This gives me error undefined offset 0. How do i print the values of images?
values returned on dd($new_products)
If you want to get the first item, you can use the first method.
{{ $new_product->images->first()->image_name }}
You also have the offsetGet method to get an item at a given offset.
{{ $new_product->images->offsetGet(0)->image_name }}
You can also loop through the collection though and do this:
#foreach ($new_product->images as $image)
{{ $image->image_name }}
#endforeach
Note: The first two method will only work if your products have images. If they don't, then Laravel will return an empty collection. The third method of looping through the collection will work in all cases.
If you want to make sure that products have images, you can use the has method.
$new_products = Product::with('images')->has('images')->orderBy('created_at', 'desc')->take(10)->get();
This will only return products that have at least one image.
Docs on collection methods: http://laravel.com/docs/master/collections#method-first
Docs on relationships: http://laravel.com/docs/5.1/eloquent-relationships
From the out it appears that $new_product is also an array of two items,
{{ $new_product[0]->images[0]->image_name }}
Edit: Thomas Kim has a better answer
Your relation (images) is a collection object, if you all() method on that then you'll get the underlying array so then you can access any item from array using the index:
{{ $new_product->images->all()[0]->image_name }}
Also toArray() method will work:
{{ $new_product->images->toArray()[0]['image_name'] }}
So, either pass the array to your view like $new_product->all() or loop it. You may check the documentation here for more information about Collection object in Laravel.
I am interested in displaying quick counts of email stats in my application, but I'm getting hung up on finding an efficient way to generate the counts. I am hoping to just use the eloquent relationship with some sort of "count where" statement inside of blade.
Getting the total count works as it should:
{{count($emails->mandrillemails)}}
but is something like this possible?:
{{count($emails->mandrillemails->msg_state == 'bounced')}}
{{count($emails->mandrillemails->msg_state == 'open')}}
Here is my block of code with the #if and #foreach statements:
#if(count($sentEmails) > 0)
#foreach ($sentEmails as $emails)
<tr>
<td>
#if(count($emails->mandrillemails) > 0)
<p><span class="badge"><i class="fa fa-paper-plane"></i> {{count($emails->mandrillemails)}}</span> <span class="badge"><i class="fa fa-exclamation-triangle"></i> {{count($emails->mandrillemails->msg_state == 'bounced')}}</span></p>
#endif
</td>
</tr>
When I attempt this, I get an undefined property error on "msg_state"
You can count a relationship like this:
$emails->mandrillemails()->whereMsgState('bounced')->count();
Since you're #foreaching things, you may want to eager load this data. This is a little more complex when you're doing counts on the relationship - see http://laravel.io/forum/05-03-2014-eloquent-get-count-relation and http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/ for some possible techniques to eager-load the count value instead of the whole list of related items.
$emails->mandrillemails()->whereMsgState('bounced')->count() should work.