Can't pass element from array to view - arrays

Trying to -unsuccessfully- pass as separated values into view from this array:
#items: array:1 [▼
0 => {#548 ▼
+"location": "Guadalajara"
+"location_id": 34
}
]
}
This array comes after a leftjoin query
$default_loc = DB::table('users')
->leftJoin('locations', 'users.location_id','=','locations.id')
->where('users.id',auth()->user()->id)
->select('location', 'location_id')
->get();
If I try to pass this array to the view via compact('default_loc') I'd been only able to show the entire array as shown above with {{ $default_loc }}, but can't separate it into $default_loc->location and $default_loc->id.
Thank you all,

Since $default_loc is an array, you should treat it as an array, so to access it you need to use the [] operator:
{{ $default_loc[0]->location_id }}
However, if you are sure that only 1 record will be returned, you can use the first() method instead of get() that will give you directly the object:
$default_loc = DB::table('users')
->leftJoin('locations', 'users.location_id','=','locations.id')
->where('users.id',auth()->user()->id)
->select('location', 'location_id')
->first();
...
{{ $default_loc->location_id }}

Is your expected result always 1 result? If yes you can change ->get() for ->first(), then you will have only a single Object.
If not, you need to iterate or access with index in the view:
#foreach($default_loc as $loc)
{{ $loc->location_id }} - {{ $loc->location }}
#endforeach
or
{{ $default_loc[INDEX]->location_id }} {{ $default_loc[INDEX]->location }}

Related

How to display array as text? - Laravel

I tried this way to show this the data but it's not working:
Blade file:
#foreach($test->item['0'] as $link)
{{ $link['item1'] }}
#endforeach
Controller file:
public function testingview()
{
$test = Test::get()->toArray();
return view('Admin.test')->with('test', $test);
}
I want to show something like that:
Item1: 123, adsf, dd, abcd
Item2: on, on, true, true
You could try something like this:
#foreach($test->item['0'] as $link)
Item 1: {{ implode(" ,", $link['item1']) }}
#endforeach
The implode function takes an array, puts it together, and returns it as a string. I encourage you to read more about implode().

How to display value of object sent by node

I have question i try to put value from nodejs by init() in angularjs, later i put to $scope.product and when i try display i have problem with:
node:
return res.render('product/product', { title: 'Express', product: products });
angular:
<body ng-controller="productsController" ng-init="productView('<%= product %>')">
<% include ../share/header.ejs %>
{{ product.title }}
{{ product.price }}
</body>
controller:
$scope.productView = function (product) {
$scope.product = JSON.parse(product);
console.log(product);
};
Error: [$parse:syntax] Syntax Error: Token 'bf' is unexpected, expecting [)] at column 55 of the expression [productView('{ _id: 5950c8902a76e81b5cc56a6f, title: 'bf', price: 7, __v: 0 }')] starting at [bf', price: 7, __v: 0 }')].
product: products - it's possible you are passing down an array of objects instead of an individual one. If that's the case, either change the res.render call on the server to pass only 1 product, or change the client-side code to expect an array and use something like ngRepeat to iterate over them.
you dont need productView function, try simply
<body ng-controller="productsController" ng-init="product='<%= product %>'">
{{ product.title }}
{{ product.price }}

Twig Markup - Pulling out Variables

Im new to twig and having a hard time getting what I need out of an array.
Here is my output from
{{ dump(items) }}
array (size=1)
0 =>
array (size=2)
'content' =>
array (size=4)
'#type' => string 'processed_text' (length=14)
'#text' => string '8AS09DF8a90sd80' (length=15)
'#format' => string 'basic_html' (length=10)
'#langcode' => string 'en' (length=2)
'attributes' =>
object(Drupal\Core\Template\Attribute)[2249]
protected 'storage' =>
array (size=0)
...
So I have an object(i think) with nested info inside of it
I am trying to pull out the '#text' of 8AS09DF8a90sd80 but cant get anything but the dump to work.
Ive tried:
{{ dump(items[0]) }}
{{ dump(items['content']) }}
{{ items.content }}
{{ items.['content']['text'] }}
and a bunch of other formats, nothing works!!
How do I structure this in twig?
This should work:
{{ items.0.content['#langcode'] }}
We need to use:
0 to select the first key of the items array
content to select the content node
#langcode to get the value with #langcode key
Where . and [] have the same role: they are used to access to an attribute of an object, in this case the value associated to a key in an array. But writing items.0.content.#langcode would trigger a syntax error because # is not a valid character (1), so we have to use the other syntax ['#langcode'].
Source: Official documentation.
(1): I didn't tested but I'm pretty sure of this.

Use angularjs filter with parameters in the json

I want to filter a list and get all the elements which have an argument equals to true. But my argument is a property and I don't know how to tell to angularjs to compute it.
{{ list | filter: {argument: true} }}
For instance if I have scope.argument = 'foo' my html should interpret it like this
{{ list | filter: {'test': true} }}
Is it possible?
I found a solution.
I create a filter in my controller
scope.isSelected = function(element) {
return element[scope.argument] === true;
}
and then I use it in my html like this
{{ (list | filter: isSelected).length }}
I would prefer to do this directly in my html but I didn't find a way to do it.

AngularJs - Subproperty filtering as AND operation

I've been trying to filter an array using Angular like this:
{{ (array | filter: { property: { subproperty: 'value' } }).length }}
and it works great.
Then I tried:
{{ (array | filter: { property: { subproperty1: 'value1', subproperty2: 'value2' } }).length }}
and noticed that Angular interprets this as an OR operation for the 2 subproperties.
How do I filter by 2 or more subproperties as an AND operation?
Thanks!
What you trying to do basically adding an additional filter to the current one. Try using the following and it should work as an AND filter.
{{ (array | filter: { property: { subproperty1: 'value1'}}
| filter: { property: { subproperty2: 'value2'}}).length }}
-- EDIT --
According to the angularjs docs:
A pattern object can be used to filter specific properties on objects contained
by array. For example {name:"M", phone:"1"} predicate will return an array of items which have property name containing "M" AND property phone containing "1".
I prepared the following filter (arguments) based on the pointers in the docs and it works perfect for me.
{{ (array | filter: { property.subproperty1 : 'value1',
property.subproperty2 : 'value2' }).length }}
functional fiddle
P.S. - I also tried to figure out why your code was acting as an OR operator, but it never worked for me. non-functional fiddle
Cheers!

Resources