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'] }}
Related
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 */
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
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
I have json column where i save my orders data in it, code below is sample of that data:
[{"id":27,"name":"new product","price":7246,"quantity":"1","attributes":[],"conditions":[]}]
with code above if i have such loop in my edit page i'm able to get orders info:
#foreach($order->product_data as $data)
{{ $data['quantity'] }}
#endforeach
until here everything is good, but the problem when comes that my order has attributes "attributes":[], then i get this error:
Invalid argument supplied for foreach()
and my data in json column is like:
"[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":\"1\",\"attributes\":[{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]"
as you see this order has 2 attributes. (can be more or less).
additional
this is how i get those data in order model:
protected $casts = [
'product_data' => 'array',
];
any idea why i get that error and how i can get my attributes data no matter if my order has attribute or not?
UPDATE
if i open edit page of order without attribute and dd the result will be like:
array:6 [▼
"id" => 27
"name" => "new product"
"price" => 7246
"quantity" => "1"
"attributes" => []
"conditions" => []
]
if load the same on order with attributes get the error above.
You should only allow to print data if exists and having the format we are expecting ,please add necessary conditions to your code like below which may prevent from getting the errors
Assuming the array you have in post is data you need to print.
#if($order->product_data) // check product_data value or not , also add key exists if needed
#if(is_array($order->product_data)) // check for product_data is array or not
#foreach($order->product_data as $data)
{{ $data['quantity'] // if array }}
{{ $data->quantity // if object }}
#if($data->attributes)
#foreach($data->attributes as $attribute) // loop through attributes
{{ $attribute->nameOfTheProperty // if object }}
#endforeach
#endif
#endforeach
#endif
#endif
But better to handle the data in controller if possible
update:
you need to convert your data into array, as your attribute property will be blank array if not having value otherwise it will be stdObject.
so your code should be : ($order->product_data should be an array if not then convert it to an array)
#if($order->product_data)
#if(is_array($order->product_data)) // assuming array
#foreach($order->product_data as $data)
{{ $data['quantity'] // if array }}
#if(count($data['attributes']) > 0) // **checked for blank array**
#foreach($data->attributes as $attribute) // loop through attributes
{{ $attribute['attr']['label'] }}
{{ $attribute['attr']['price'] }}
#endforeach
#endif
#endforeach
#endif
#endif
Tried with your sample data, and that executed without any error.
$json = "[{\"id\":29,\"name\":\"effewf\",\"price\":24524,\"quantity\":\"1\",\"attributes\":[{\"attr\":{\"label\":\"Red\",\"price\":\"5000.00\"}},{\"attr\":{\"label\":\"22\\\"\",\"price\":\"900000.00\"}}],\"conditions\":[]}]";
$jsonAsArray = json_decode($json, true);
foreach ($jsonAsArray as $data) {
echo $data['quantity'] . "\n";
if (! empty($data['attributes']) ) {
if (is_array($data['attributes'])) {
foreach ($data['attributes'] as $attribute) {
echo $attribute['attr']['label'] . ' ' . $attribute['attr']['price'] . "\n";
}
}
}
}
Outputs:
1
Red 5000.00
22" 900000.00
In your case, the product_data is not cast as an array.
You could try Defining An Accessor in your Order model, and remove the cast property of product_data
public function getProductDataAttribute($value)
{
return json_decode($value, TRUE);
}
Or Manually decode the json in your loop
#foreach (json_decode($order->product_data, TRUE) as $data)
{{ $data['quantity'] }}
#if (! empty($data['attributes']))
#foreach ($data['attributes'] as $attribute)
{{ $attribute['attr']['label'] }} {{ $attribute['attr']['price'] }}
#endforeach
#endif
#endforeach
This is how I did mine (category is a json column in the DB):
<div class="category">
<ul class="list-group">
#foreach ( json_decode( $code->category ) as $category)
<li class="list-group-item d-flex justify-content-between align-items-center">
<span class="badge badge-secondary badge-pill">{{$category}}</span>
</li>
#endforeach
</ul>
</div>
You just have to json_decode your json column to get an array and iterate over it. that's it, cheers.
I know there are many questions on this topic, but none quite deal with this (as far as I could see).
I have a PHP array (which FYI, is returned via Guzzle response) in a Laravel Project.
The PHP array
$users = array(2) {
["error"]=>
bool(false)
["spirits"]=>
array(2) {
[0]=>
array(2) {
["id"]=>
string(1) "1"
["name"]=>
string(5) "Foo"
}
[1]=>
array(2) {
["id"]=>
string(1) "2"
["name"]=>
string(3) "Bar"
}
}
}
I simply want to extract the "id" and "name" keys below, to use in a view but I'm a little stumped. I've tried the suggestions below, but can't quite work it out.
How to Flatten a Multidimensional Array?
PHP foreach with Nested Array?
I've also looked into array_walk_recursive.
Any help would be awesome and appreciated! I want to be able to use these 2 keys in Laravel like so:
Controller
return View::make('users')->with('users',$users);
View
#foreach ($users as $key => $user)
{{ $user["id"] }}
{{ $user["name"] }}
#endforeach
You may try this:
#foreach ($users['spirits'] as $user)
{{ $user["id"] }}
{{ $user["name"] }}
#endforeach
It's better to check the returned result in your controller before you send it to the view using something like this so there will be no errors in your view:
$users = 'Get it from somewhere...';
if(!$users['error']) {
return View::make('users')->with('users', $users);
}
else {
// Show an error with a different view
}
in case your users are always stored in the spirits-key of your $users variable you simply could modify your #foreach-loop as follow:
#foreach ($users['spirits'] as $user)
{{ $user['id'] }}
{{ $user['name'] }}
#endforeach
Otherwise you could edit your return value from the controller. That means you simply could change the line:
return View::make('users')->with('users',$users);
to
return View::make('users')->with('users',$users['spirits']);
In this case you don't have access to your error-key.