Accessing array levels in laravel blade - php

I have an array which shows below code after dump.
array:1[
"123"=>array:3[
"test1" => 12345
"test2" => "test"
"test3" => 123
]
]
I am trying to access each element in an html table and it's not showing my values. The code i am using is,
#foreach($testNumbers as $numbers)
<tr>
<td>{{$numbers['123']->test1}}</td>
<td>{{$numbers['123']->test2}}</td>
<td>{{$numbers['123']->test3}}</td>
</tr>
#endforeach
Can you please tell what i am doing wrong here?

This is a multidimensional array. Try using
{{$numbers['123']['test1']}}

This is array of array (not of object) so you have to do like this:
#foreach($testNumbers as $numbers)
<tr>
<td>{{$numbers['test1']}}</td>
<td>{{$numbers['test2']}}</td>
<td>{{$numbers['test3']}}</td>
</tr>
#endforeach
OR
#foreach($testNumbers as $numbers)
<tr>
#foreach($numbers as $number)
<td>{{$number}}</td>
#endforeach
</tr>
#endforeach

Related

Data Pass from controller to view

I want to display the sum of column debit. When I pass data after sum value of the debit column and store it in a variable, I pass the data from controller to my view but laravel says it's undefined. Can anyone help me with this?
View.blade.php
<tr>
<th>Total Debit:</th>
<td>
#foreach ($totaldebit as $tdebit)
{{ $tdebit }}
#endforeach
</td>
</tr>
UserController.php
$tdebit = DB::table('debitemps')->sum('debit');
return view('pages/payments/debit/debit', [
'workerlists' => $data,
'debitid' => $id,
'Empdebitdata' => $dataEmp,
'totaldebit' => $tdebit
]);

why does't explode function work in laravel project?

It is working in the editor and gives a expected output.
But when running the project, it would not get expected output.
code:
#php
$pro ="cat-19,subcat-52,subcat-55";
$var=explode(",",$pro);
#endphp
#foreach($var as $row)
{{$row}}
#endforeach
What could be the problem in my code?
I guess you want to have a preview of the array.
$pro ="cat-19,subcat-52,subcat-55";
$var=explode(",",$pro);
print_r($var);
/* this will give you an associative array
result from print_r will be Array ( [0] => cat-19 [1] => subcat-52 [2] => subcat-55 ) */
#endphp
#foreach($var as $row)
{{$row}}
#endforeach
/* inside the loop since you are printing the value you will get the output as,
cat-19 subcat-52 subcat-55 */

Laravel php combine two foreach

In my blade.php file, I'm trying to merge two foreach loop to display data in the table. I had tried use something like nested loop but it doesn't work.
#foreach($cat->products as $idx => $product)
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
</tr>
#endforeach
#endforeach
I just need to insert the total data into the table. Now the total is displayed correctly but it will duplicate the sku and name in the table.
Define an array which we will use as a "flag" and on each iteration check if the current product SKU is NOT in our "flag" array. If it isn't then we display and we add the current product SKU to the list of processed SKUs and continue as normal.
#php $skus = [] #endphp
#foreach($cat->products as $idx => $product)
#if (!in_array($product->sku, $skus))
<tr ng-init="item.items[{{$cat->id}}][{{$idx}}] = {};">
#foreach ($queryInv as $querInvs)
<td>{{$product->sku}}</td>
<td>{{$product->name}}</td>
<td class="text-right">{{{$querInvs->total or 0}}}</td>
#endforeach
</tr>
#php array_push($skus, $product->sku); #endphp
#endif
#endforeach
Note: You are using Laravel 4, the current version of Laravel is 5.7, I would absolutely update and use the latest version.
Reading Material
in_array
array_push

How to echo array elements in laravel

I want to echo arry elemnts in view
followings are my code (controller)
$next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get();
return view('invoice.view')->with('next_d_dts',$next_d_dts);
I can print it using print function, eg:
print_r($next_d_dts);
Output:
Array ( [0] => stdClass Object ( [next_due_bill_date] => 2019-03-28 ) [1] => stdClass Object ( [next_due_bill_date] => 2020-02-28 ) )
You need to iterate over the collection of objects:
#foreach ($next_d_dts as $object)
{{ $object->name }}
#endforeach
If you just want to see its contents but not stop the script:
{{ var_dump($next_d_dts) }}
You've also asked how to iterate without Blade:
foreach ($next_d_dts as $object) {
echo $object->name;
}
#foreach($next_d_dts as $value)
{{$value['next_due_bill_date']}}
#endforeach
you should used foreach loop in laravel like this
#foreach ($next_d_dts as $value)
<p>Some text here{{ $value->id }}</p>
#endforeach
for more information read Laravel Manual blade template
Also you can used
dd($next_d_dts) //The dd function dumps the given variables and ends execution of the script
You can convert it into array using toArray() and iterate over it
$next_d_dts= \DB::table('tb_billing_cycle')->select('next_due_bill_date')->where('customer_id',$row->customer_id)->get()->toArray();
In view
#foreach($next_d_dts as $value)
{{ $value['column_name'] }}
#endforeach
Or
print_r($next_d_dts)
#foreach($array as $item)
{{$item}}
#endforeach
Simple.
you need to use Blade Loops syntax invoice.view.blade.php
#foreach($next_d_dts as $next )
{{ $next }}
#endforeach

Cannot loop through custom array

Using Laravel 4 and trying to create an array of objects without using eloquent.
That array is made this like:
$item_collection[] = [
'item_type' => $item->item_type->name,
];
This returns a long list of items in an array:
array(258) {
[0] array(1) {
["item_type"] "Deal Bag"
}
[1] array(1) {
["item_type"] "Car"
}
[2] array(1) {
["item_type"] "Deal Bag"
}
}
In my view I am trying to loop like normal:
#foreach ($items as $item)
<tr>
<td>{{ $item->item_type }}</td>
</tr>
#endforeach
But receiving "Trying to get property of non-object" for all the values.
Where am I going wrong either with making / looping this array?
Try using array notation:
{{ $item['item_type'] }}

Categories