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
Related
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 have 5 columns in my database. Here are the names of the columns:
promo1
promo2
promo3
promo4
promo5
I would like to combine these columns to be able to make a foreach in my view.
I tried this:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->where('id', $card->id)->get()->toArray();
and in my view :
#foreach($promos as $promo)
<span>{{ $promo['promo1'] }}</span>
#endforeach
But I have only one result for my foreach. Instead of 5. And I can only select one by one ($ promo ['promo1'], $ promo ['promo2']) so doing a foreach would not help
Is there a way to do that? thank you very much
When you're using get() method, you're getting a collection. Use the find() method to get an object instead of collection:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->find($card->id)->toArray();
Then you'll be able to iterate over promos:
#foreach ($promos as $promo)
<span>{{ $promo }}</span>
#endforeach
First get the building object
$building = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->findOrFail($card->id);
then loop through the promos
#foreach ($building->getAttributes() as $key => $value)
<span>{{ $key }} : {{ $value }}</span>
#endforeach
note: using the $key here is optional
I'm trying to get a specific value using an array key, but I can't seem to figure out how to make it work.
$Array = Array(
"key1" => "value1",
"key2" => "value2"
);
Let's say I want to get the value of "key1" only, and I return the array with the view.
return view("myview")->with("arraytoprint", $Array);
And I try it with blade but I get Trying to get property of non-object...
#foreach($arraytoprint as $arr)
{{ $arr->key1 }}
#endforeach
How can this be achieved?
The error tells you that you are trying to get property of non-object but you are passing in an array. The -> notation is used for accessing properties in objects, not arrays.
You access keys in PHP arrays using the square bracket notation instead, as below:
$arr['key1']
In your example you also have no need for the foreach loop if you want to access the keys directly, just simply do:
{{ $arraytoprint['key1'] }}
If you do want to loop over the values then you can just do the below:
#foreach($arraytoprint as $value)
{{ $value }}
#endforeach
And for completeness sake, if you want the keys too you can do:
#foreach($arraytoprint as $key => $value)
{{ $key }} : {{ $value }}
#endforeach
In Laravel 5, passing data to the view is now done like this :
return view("myview", ["arraytopoint"=>$Array]);
and access in blade view like this:
{{$arraytopoint['key1']}} inside #foreach loop
or test value of variable like this :
<?php print_r($arraytopoint['key1']); ?>
I am displaying elements of an array #foreach($tags as $tag)$tag->#endforeach. The output is tag1tag2tag3. What is the possible way to sho elements of array in tag1,tag2,tag3. And how to not show, if there is only one element in array.
The selected answer is too complicated. Laravel has a simpler solution:
{{ $items->pluck('tag')->implode(', ') }}
implode() is good for echoing simple data. In real project you usually want to add some HTML or logic into the loop, use $loop variable which is available since 5.3:
#foreach ($arrayOrCollection as $value)
{{ $loop->first ? '' : ', ' }}
<span class="nice">{{ $value->first_name }}</span>
#endforeach
Use this. We can implement it using $loop->last
#foreach ($arrayOrCollection as $value)
<span class="nice">
{{ $value->first_name }}
#if( !$loop->last)
,
#endif
</span>
#endforeach
Use implode:
{{ implode(', ', $tags) }}
implode is one option or you can using join as well like this
{{ join(', ', $tags) }}
Try the first one or this one..
good luck
I believe what you are looking for might be something like this:
//have your array in php tags
//$arr = ['one', 'two', 'three']; ? >
//go through the array with foreach and if the count of the array is not equal to the las element then put coma after it
#foreach ($arr as $key => $value)
#if( count( $arr ) != $key + 1 )
{{ $value }},
#else
{{ $value }}
#endif
#endforeach
Try implode():
$arr = ['one', 'two', 'three'];
echo implode(',', $arr);
// output
one,two,three
hello people here is my code below for controller
$siteinformation = DB::table('siteinformation')->where('Site',$myarr[0]['site'])->select()->get();
print_R($siteinformation); will display
Array ( [0] => stdClass Object ( [Site] => site1 [Nickname] => friend [ProgramID] => 1 [CreateDateTime] => 2014-06-03 18:05:39 ) )
I am trying to pass it to view
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
In my view i have...
#if(Session::has('siteinformation'))
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif
Iam gettng an error Undefined variable: siteinformation
my route file contains... Route::post('sbs_site', array('uses' => 'myController#sys_config'));
what could be the problem??
I think you don't need to redirect; probably you are doing it wrong, instead you should pass the data to the view directly using something like this:
$siteinformation = DB::table('siteinformation')
->where('Site',$myarr[0]['site'])
->get();
// View Name: "sbs_site.blade.php"
return View::make('sbs_site')->with('siteinformation', $siteinformation);
Then in the view:
#foreach ($siteinformation as $value)
{{ $value->Site }}
{{ $value->Nickname }}
#endforeach
But if your really need to use a redirect then you may do it like this:
// Route Is: "sbs_site"
return Redirect::to('sbs_site')->with('siteinformation', $siteinformation);
Then in your view:
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation') as $value)
{{ $value->Site }}
{{ $value->Nickname}}
#endforeach
#endif
This worked for me...
in view
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation')as $key => $value)
{{ $value->Nickname}}
#endforeach
#endif
In controller
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. You set in your controller 'siteinformation' as a normal variable. If you want to set session, instead of this.
Redirect::to('sbs_site')->with('siteinformation',$urserializedobj);
Do this
Session::put('siteinformation',$urserializedobj);
Redirect::to('sbs_site');
OR
Simply don't check session in your view instead of this
if(Session::has('siteinformation'))
Just do this
if (!empty($siteinformation)) :
I don't know why u are redirecting to pass it to the view
return View::make('yourview',array('siteinformation'=>$siteinformation));
This will do the trick if you can make the view directly from the controller
BUT
if u need to redirect than i suppose you can serialize your array or change it to json first because the with function passes the data as flash so only strings
$urserializedobj = $siteinformation.toJson();//if this doesn't work try serealizing
return Redirect::to('sbs_site')->with('siteinformation',$urserializedobj );
now in your view you can do this
#if(Session::has('siteinformation'))
$siteInformation = json_decode(Session::get('siteinformation'));
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif