why does't explode function work in laravel project? - php

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 */

Related

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

Get specific key value from array

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']); ?>

Outputting a Multidimensional Array

I'm trying to call this array but I'm getting an Array to string conversion Error. I'm not sure what I'm doing wrong, I'm just learning. If anyone could help me get this set up correctly I would greatly appreciate it!
Array:
public function getPricing()
{
$windows = array("Storm Windows" => array("(includes removal, cleaning,
and replacement)", " - $12.00")
, "Double Hung" => array("(opens up and down)" ,
" - $9.00")
, "Casement" => array("(crank out or stationary
window)" ," - $8.00")
, "Transom" => array("(rectangle window over top of a
window)" ," - $3.00")
);
return view('pages.pricing')
->with("windows", $windows);
}
Calling it in the view:
#foreach ($windows as $window => $DescriptionPrice)
<h2>{{$window . $DescriptionPrice}}</h2>
#endforeach
Try this code: You were tying to output an array which is not a string. So, you need to select the array item you need to print out as shown below.
#foreach ($windows as $window => $DescriptionPrice)
<h2>For {{ $window }} - {{$DescriptionPrice[0]}} - {{$DescriptionPrice[1]}}</h2>
#endforeach
$DescriptionPrice is an array so can't be output as a string. You will need to do something like this instead:-
#foreach ($windows as $window => $DescriptionPrice)
<h2>{{$window}} {{$DescriptionPrice[0]}} {{$DescriptionPrice[1]}}</h2>
#endforeach

error of foreach with empty array

I'm working in Laravel 5 using Blade as motor of templates. I'm passing an array from the controller to the view, and I noticed that when I loop on it using the foreach clausule and the array is empty it gives error, exactly this:
Invalid argument supplied for foreach()
I had the same error in the controller and I fix it temporaly making:
if(count($student)!=0)
I said temporaly because I don't think it this the best way to do it.
The code in the controller is:
foreach($students as $student){
if(count($student->contracts)!=0)
foreach($student->contracts as $contract){
//something
}//end foreach
}//end foreach
I made some operations over the arrays, and then I send them to the view:
return view('myview')->with(['students'=>$students]);
The array is passing to the view correctly. I said is the foreach, beacause earlier I had the database full of registers and it worked fine, but now I have some students that doesn't have contracts and then I got that error. But, in the view I have the same error. So, it's normal? how could I fix it in a better way? why when the array is empty the foreach clausule gives that error?
PHP will not return that warning if the array contained at $student->contracts is empty. It will return it if it is of an invalid type (i.e. not an array).
Rather than checking the count() of $student->contracts, you'd be better to check if it's actually an array, as follows:
foreach($students as $student)
{
// Make sure that $student->contracts is actually an array (to bypass errors):
if( is_array($student->contracts) )
{
// Now loop through it:
foreach( $student->contracts as $contract)
{
// Do something here
}
}
}
Try this
$people = [
"Person A", "Person B", "Person C"
];
return view ('pages', compact('people'));
and loop through it like this:
#if (count($people))
<h3>People:</h3>
<ul>
#foreach($people as $person)
<li>{{ $person }}</li>
#endforeach
</ul>
#endif

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