i have key and value datas. key is an array. i want to foreach this key data. I use laravel 5. my json_decoded array like :
Collection {#1288 ▼
#items: array:4 [
"{"id":1,"title":"abc","path":"abc-path"}" => 19
]
}
But i can not fetch key data like i wanted my code :
#foreach($trendings as $key => $value)
{{ $key->id }}
#endforeach
it gives ' Trying to get property of non-object ' error. but if write code like :
#foreach($trendings as $key => $value)
{{ $key }}
#endforeach
it gives me
{"id":1,"title":"abc","path":"abc-path"}
but i want them use in my html. how can i fetch them ?
In my opinion, view should not execute a json_decode.
I expect view to get already decoded variables, i think that if your data is not completly decoded something is missing before the view.
Of course i would rather work on fixing what's wrong before than patching your view.
Related
I would like to retrieve an element by the id.
inside my blade file:
{{ $auctionStatuses }}
This outputs:
[
{
"id":1,
"name":"Awaiting customer action",
"created_at":"2018-10-04 10:14:08",
"updated_at":"2018-10-04 10:14:08"
},
{
"id":2,
"name":"Transfer in progress",
"created_at":"2018-10-04 10:15:11",
"updated_at":"2018-10-04 10:15:11"
},
{
"id":3,
"name":"Completed",
"created_at":"2018-10-04 10:15:14",
"updated_at":"2018-10-04 10:15:14"
}
]
I would like to output the name when referencing by the id field
Due to some data coming from mongodb, i can't use the standard relationships
I have tried this {{ $auctionStatuses['3'] }}
but that doesn't exist, obviously.
i guess ideally, i would like to do something like this
{{ $auctionStatuses[*]->id['3']->name }}
Is there a way to do what i need, without looping through the json array?
With one line:-
$auctionStatuses->toArray()[array_search(3,array_column($auctionStatuses->toArray(),'id'))]['name']
Explanation
toArray() Method converting a laravel collection to an array.
array_search Will searching for a array element and returning the index
array_column will returning values with specify column
You can use :
$auctionStatuses->firstWhere('id',3)->name;
https://laravel.com/docs/5.7/collections#method-first-where
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'm new to laravel
Just being curious,
Suppose I make an array in a view page
myView.blade.php
Suppose:
$array = [
1, 2, 3
];
in the same page, I want to loop it with "blade" foreach
suppose:
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
but, I get an error like this.
ErrorException in 1ed42d9dadecab7c54e086f573c4cbad6576e7c3.php line 63:
Trying to get property of non-object...
what's happened? what type of variable actually blade converts?
Any question will highly appreciated! :)
First of all, it's better if you just pass the data to your view files from controller.
But, if you still want to do that, and you are using Laravel 5.3, you can do it like this:
#php
$array = [
1, 2, 3
]
#endphp
and then loop it:
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
Make, sure that you declare the $array array before the loop.
Have a look at the Service Injection and View Composers on Larave's documentation aswell.
Actually, I don't know what do you want? But recommendly, you should init your array at controller, through it to view and foreach.
In controller,
return view('your view path', ['array' => $array]);
In blade
#foreach($array as $value)
<span id="{{$value}}">{{$value}}</span>
#endforeach
Hope it could help, thanks.
I am trying to figure out how I can give my data output in the blade file, the Laravel look;
like $data->name
But I can't get the output to be casted as an object. I think I have to make an array of the data before I can loop it proper in a foreach but this doesn't feel like the right way.
I am relatively new to Laravel and I want to do this the nice way, can someone point me in the right direction? Thanks in advance
Controller:
$data = collect($this->api->organization->index())->toArray();
return View::make('pages.organization.index', array('data' => $data[0]));
View:
#foreach($data as ((object)$organization))
{{ $organization->name }}
#endforeach
I know this will not work, but I think it illustrates my question a little bit.
EDIT
What I didn't realize is that $data = collect($this->api->organization->index()); is returning an array with all the data arrays inside because I didn't name it in my return like this:
return (object)['all' => $data];
After adding all I could reference the code inside my view like I wanted to. I know this is not a very detailed answer, if you run into the same problem message me I'll edit the answer.
Object:
$data = collect($this->api->organization->index());
#foreach($data as $organization))
{{ $organization->name }}
#endforeach
Array:
$data = collect($this->api->organization->index())->toArray();
#foreach($data as $organization))
{{ $organization['name'] }}
#endforeach
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