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.
Related
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.
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've got a problem in Laravel. I have passed my whole table to my view like this from my controller:
$usersTable = DB::table('users')->get();
return view('users')->with('users', $usersTable);
In a foreach loop I can perfectly get each of the values like this in my view:
#foreach($users as $user => $value)
<div class="projectBox">
<br><span class="projectBoxName">{{ $value->name }}</span>
#php
echo Form::image('/images/edit.png', "",['class' => "editUserBtn", 'userId' => $value->id]);
#endphp
<br><span class="projectBoxSmallText projectBoxEmail">{{ $value->email }}</span>
<br><span class="projectBoxSmallText projectBoxId">ID: {{ $value->id }}</span>
<br><span class="projectBoxSmallText projectBoxProjects">Currently no projects</span>
</div>
#endforeach
But I also need to access these values outside my foreach loop, how can I do that?
echo Form::text('email', "$users->$value->email", array('placeholder' => "Email"));
Ain't working...
This gives my the whole object in this form
[{"id":"1","name":"Administrator","email":"admin#mail.com","password":"$2y$10$Re3Ahf.SwU5vj4UvtU5Dy.jxaZMsUNC2WhuJMwsNy9gu6TST4PuRG","remember_token":null}]
How to get only the email? I also tried using indexes, but those weren't working.
Thanks!
Edit:
Full situation:
I have a list of users with their extra information (mail, tel,...). In those user-boxes there is a button which says 'edit user' when I click that a modal opens giving the current mail and tel. So I can't say in my controller WHO's mailaddress to return because I only know that at the moment the user clicks a client-side button.
Images: http://imgur.com/a/krDrY
(Edit button is that small circle with three dots).
To access a collection without using loop, you should use collection methods:
$users = User::get();
$users->where('name', 'John Smith')->first()->email
This will not create any additional queries since you've already eager loaded data.
If you want to load just one user, use first() instead of get():
$users = User::first();
If you'll use get() and then [0] or first() like some guys recommend here, you'll load all users data into the memory for each request which will overload your server.
Also, using indexes to access data (like $users[4]['email']) is a bad practice. Avoid it if possible.
You need to add as first element by adding [0]
echo Form::text('email', $users[0]->email, array('placeholder' => "Email"));
I suggest use ->first() instead of ->get() to get single object. And remove loop and use anywhere you want.
You are using a collection of users to get the first user's email, you do
$users->first()->email;
Looks like it's coming in as JSON. Try json_decode($data) and $data->email to get that attribute.
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