Sort array by email in Twig - sorting

I have an array of users that I'm looping trough to display on a page.I want to sort the users by their email, but I cant figure out how to do this, I've read trough som of the other questions, but cant get them to work. Any ideas?
{% for group in person.teachers %}
{{person.img}}
{{person.name}}
{{person.description}}
{{person.email}}
{% endfor %}

Related

Update Change Password Page For Default And Other Groups

I am using opencart V 3.0.3.0
I want to check if customer group id = default, then show him change password form and allow to change password. Else show table with links to other platforms on my website to change password.
Table with links to other platforms for password change is done by me and it is visible on change password page as expected.
But I not getting how to check customer group id and show form to change password ? which files from catalog/controller and catalog/model needed to change and what can be code on password.twig
e.g. code in password.twig
{{ if customer_group_id==1}} // 1 = default
code to show change password form
{{ else }}
show my created table with links to other platforms on my website.. (it is done and working as expected right now )
{{ end if }}
So after some search, I did it successfully.
Giving solution for others who may needed it
in catalog/controller/account/password.php added
if ($this->customer->isLogged()) {
$data['customer_group_id'] = $this->customer->getGroupId();
} elseif ($this->config->get('config_customer_group_id')) {
$data['customer_group_id'] = $this->config->get('config_customer_group_id');
}
after line 12 i.e.
$this->load->language('account/password');
And in catalog/view/theme/default/template/account/password.twig
added
{% if (customer_group_id=='1') %}
// Your needed code
{% else %}
// another needed code
{% endif %}
after
<h1>{{ heading_title }}</h1>

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

Opencart unable to display variable sent from controller in view

I am having difficulty getting a variable to display on the product page in Opencart. I have added a SQL query to catalog/model/catalog/product.php file as below:
public function getLargeItems($product_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "oc_product_attribute WHERE product_id = '" . (int)$product_id . "' AND attribute_id=17 AND (text LIKE '10lt' OR text LIKE '30lt' OR text LIKE '50lt')");
return $query->row;
}
I then added the following to the catalog/controller/product/product.php file:
$large_items = $this->model_catalog_product->getLargeItems($product_id);
Finally I added the following to the /catalog/view/theme/mytheme/template/product/product.tpl page:
<?php if ($large_items) { ?>
<h1>Tree</h1>
<?php } ?>
And it returns Undefined variable: large_items in /catalog/view/theme/mytheme/template/product/product.tpl
I am at a bit of a loss now. Is anyone able to see by my code why its unable to pull the variable from the controller?
I've tried simplifying it by adding $large_items = "Hello" to the controller to see if I can echo this on the view page however it still cant see the variable.
The version of Opencart is 2.3.0.2
Thank you for your time.
I managed to resolve this by doing two things:
Removing oc_ from the query as the prefix is already defined, I was however only able to notice this after discovering that controller changes dont get picked up without clearing the cache in the admin panel > modifications > click the blue refresh button.
I was then able to see the broken query in the php logs.
The issue was my shoddy code, but I hope that the cache refresh information is useful to others if they get stuck.
Thanks
First you must remove oc_ from "oc_product_attribute WHERE product_id =... because prefix is already defined like " . DB_PREFIX . ".
Next you not properly defined $large_items in your controller. must be:
$data['large_items'] = $this->model_catalog_product->getLargeItems($product_id);
then you can get it on your .tpl like $large_items or in .twig {{ large_items }}
in twig your code should be:
{% if large_items %}
<h1>Tree</h1>
{% endif %}
Good luck

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;

Laravel returns undefined offset 0 when trying to print from array

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.

Resources