Looping through a collection with an array - Laravel - php

I have a collection which I have extracted from the database using the findMany method in eloquent.
When I dd the collection it looks like the attached image. And then I am trying to (in my blade) loop through each item in the array such that I am able to print each of the values in the actual records.
$question = Question::whereIn('id', [23,25])->get();
Then in my blade I am trying to do:
#foreach ($question as $row => $innerArray)
#foreach ($innerArray as $innerRow => $value)
{{$value->question}} //I was expecting "What is your favourite food" here
#foreach ($value->option as $choice)
<li>{{$choice}}</li> //I was expecting the list of the options here
#endforeach
#endforeach
#endforeach
What am I doing wrong?

All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.
All collections also serve as iterators, allowing you to loop over them as if they were simple PHP arrays:
$questions = Question::whereIn('id', [23, 25, ..])->get();
foreach ($questions as $question) {
echo $question->title; // What is your favourite food
echo $question->name; // food
foreach ($question->options as $option) {
echo $option; // Pizza
}
}

The correct syntax is this:
#foreach ($question as $q)
{{ $q->question }} //I was expecting "What is your favourite food" here
#foreach ($q->options as $choice)
<li>{{$choice}}</li> //I was expecting the list of the options here
#endforeach
#endforeach

Related

Laravel Eloquent fetch nested compact json data

I have two different Models
App\Models\Team;
and
App\Models\Member;
Where my Team Model has this function
public function memberList()
{
return $this->hasMany('App\Models\Member');
}
and I'm using this code to show it on my blade
$team = Team::with('memberList')->get();
return view('front.pages.custom-pages-index', compact('team'));
here's my columns for the tables
MemberTable
Team Table
Now I'm trying to access the compact data using this foreach
#if(count($team))
#foreach($team as $field)
//Name of Team
<h1>{{$field->name}}</h1>
//Member
....
#endforeach
#endif
This is the sample content of compact data when I tried to access the {{ $team }} on blade file
[{
"id":1,
"name":"Executive Team",
"created_at":"2020-05-26 04:38:27",
"updated_at":"2020-05-26 04:38:27",
"member_list":[{
"id":1,
"team_id":1,
"position":1,
"name":"Chris White, PH.D",
"member_position":"President, Founder and CEO",
...And other member per specific team
The problem is I want to fetch on blade the members per specific teams
UPDATE
When I tried at least to access the name inside of the member_list using foreach
$field->member_list->name
I'm getting an error like this
Trying to get property 'name' of non-object
Tried to var_dump($field->member_list)
Tried this #php dd($field->member_list); #endphp
Because member_list item of variable field is an array and "name" isn't a key of this array. You should use another loop for your member list inside of your main loop.
#if(count($team))
#foreach($team as $field)
//Name of Team
<h1>{{$field->name}}</h1>
//Member
#if(!empty($field->member_list) OR !is_null($field->memberlist))
#foreach($field->member_list as $member)
<h2>{{$member->name}}</h2>
#endforeach
#endif
#endforeach
#endif
In my Member.php (Model)
Added this
public function member()
{
return $this->belongsTo('App\Models\Team', 'id', 'team_id');
}
In my Team.php (Model)
public function memberList()
{
return $this->hasMany('App\Models\Member', 'team_id', 'id');
}
Controller
$team = Team::with('memberList')->get();
return view('front.pages.custom-pages-index', compact('team'));
Blade
#if(count($team) > 0)
#foreach($team as $field)
{{$field->name}}
#foreach($field->memberList as $member)
{{$member->image}}
#endforeach
#endforeach
#endif
Need to map first the Team and Member models using its Primary key and Foreign Keys
And call the json column memberList

Trying to get object of non-object. (Laravel)

I'm confused here. I've passed
$grade=Grade::all()->whereLoose('id',$gid);
from my controller.
When I access it in my view like:
#foreach($grade as $grade)
{{$grade->grade_name}}
#endforeach
It works fine. But when i try to use it again in my table:
#foreach($grade as $grade)
<tr class="success">
<td></td>
<td><b>Balance</b></td>
<td>{{$grade->fee_status}}</td>
#endforeach
It just throws Trying to get object of non object. What's the problem here? Can anyone help me?
You are reassigning $grade in your loop. It is now a single model. When you try to now iterate that you are iterating a single model which is not what you want to do (that will iterate the public properties of the object).
This will avoid that issue:
$grades = Grade::all()->whereLoose('id',$gid);
// different name for the variable for the current iteration
#foreach ($grades as $grade)
...
#endforeach
#foreach ($grades as $grade)
...
#endforeach
Watch the naming of variables.
On a side note, I am not sure why you are getting all the grades to just find the one with 'id' equal to $gid. If the id field is unique there should only be one and you wouldn't need to retrieve a collection.
$grade = Grade::find($gid);

Using in_array on collections

I need to check for a name in an array of names but I having trouble passing an array instead of a collection to the in_array() method.
My blade code looks something like this
#foreach($ecn->areas as $area)
{{ $area->area }}:<br>
<ul>
#foreach($area->people as $person)
#if(in_array($person, $ecn->signatures->name ))
<li><del>{{ $person }}</del></li>
#else
<li>{{ $person }}</li>
#endif
#endforeach
</ul>
#endforeach
I know my problem is in the way im trying to access the list of signatures.
#if(in_array($person, $ecn->signatures->name ))
I can access them in another part of the page doing this
#foreach($ecn->signatures as $signature)
{{ $signature->name }}<br>
#endforeach
and everything is fine.
How can I access and pass the list of signatures as an array in this scenario?
If you want to use in_array() version for Laravel Collections then you can use:
$collection->contains($needle)
It woks just like in_array.
If $needle is an integer, an id for example, don't forget to pluck first, like so:
$collection->pluck('id')->contains($needle)
You can use the lists or pluck method on the signatures collection to get a new collection of all the names. From there, you can either use the contains() method on the collection, or you can use the all() method to turn the collection into an array and use in_array directly.
I would suggest doing this outside of the foreach loop, so you're not generating this new collection and array every iteration.
Array:
// controller
$names = $ecn->signatures->lists('name')->all();
// blade
#if(in_array($person, $names))
Collection:
// controller
$names = $ecn->signatures->lists('name');
// blade
#if($names->contains($person))
You can also use contains() with a closure, which would let you get away with not creating an intermediate collection, but it's a little harder to read:
Contains with closure:
// blade
#if($ecn->signatures->contains(function ($key, $value) use ($person) {
return $value->name == $person;
}))
As per Laravel Docs
The get method returns an array of results where each result is an instance of the PHP StdClass object.
So you must convert your stdClass object to a array in your controller itself.
foreach ($ecn->signatures as $signature)
$nameArray[] = $signature->name;
and then you can use in_array in the blade template
#if(in_array($person, $nameArray))

Use variable outside foreach laravel

I have a problem with Laravel. So what am I trying to do? I'm trying to get the location from all my examples. This actually works, so as you can see I get the id of all the examples and find the locations of it from another table in my project. The var_dump in the foreach gives what I need. But I need this data outside the foreach, I need to send it to my view afterwards. But the second var_dump, the one outside the foreach only gives the location of the first id.
So my question: is there a way to get the full $locations outside the foreach.
$ids= DB::table('example')
->select('id')
->get();
foreach($ids as $id)
{
$locations = example::find($id->id)->locations()->get();
var_dump($locations);
}
var_dump($locations);
This is my view:
#foreach($examples as $example)
<h1>{{$example->name}}</h1>
#foreach($locations as $location)
{{$location}}
#endforeach
#endforeach
I see al the locations from al the $examples in every $example if I print it like this, I hope you understand my question.
Try this:
// Define locations array
$locations = [];
// Then do the following for each example:
// Define the array for locations of each example outside of the loop
$locations[$example->name] = [];
foreach ($ids as $id) {
// Add each location to the $locations array
$locations[$example->name][] = example::find($id->id)->locations()->get();
}
Or if you want to get a little bit more fancy, you can use array_map instead of foreach:
$locations[$example->name] = array_map(function ($id) {
return example::find($id->id)->locations()->get();
}, $ids);
Then in the view, you simply take out the locations from the right key for each example:
#foreach ($examples as $example)
<h1>{{ $example->name }}</h1>
#foreach ($locations[$example->name] as $location)
{{ $location }}
#endforeach
#endforeach
You don't have to use the $example->name as the key, just make sure it's unique for each example you want to fetch locations for.
I hope this helps.
There is no need to build the $locations array. You can use the relationship in the view just fine.
In your controller:
// whatever your logic is to get the examples
$examples = example::with('locations')->get();
In your view:
#foreach($examples as $example)
<h1>{{$example->name}}</h1>
#foreach($example->locations as $location)
{{$location}}
#endforeach
#endforeach

Showing a JSON with the following structure in Laravel

Good afternoon
I recieve this JSON
{"id":130,"nif":"47812480B","name":"Bernat","cognoms":"Fernandez","file":"uploads\/fondo.jpg","birthday":"0000-00-00","presentacion":"presentacion","email":"bernat#gmail.com","password":"$2y$10$GfP1DYkTqyohjVzRcuuxaeKBSu7iJUBWJK6UDj7p681307uI6Ersq","idempresa":1,"id_poblacion":889,"id_online":0,"remember_token":null,"created_at":"2015-05-28 15:25:04","updated_at":"2015-05-27 15:25:04","municipio":{"id":889,"idprovincia":33,"poblacion":"Art\u00e9s","poblacionseo":"artes","postal":8271,"latitud":"41.798479","longitud":"1.954828"},"subastas":[{"id":16,"nombre":"HTC Wildfire","descripcion":"Descripcion","precio_salida":125,"cant_actual":125,"id_estado":0,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":130,"id_ganador":125,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-13","data_final":"2015-06-13","durada":1,"created_at":"2015-06-06 09:09:54","updated_at":"2015-06-06 09:55:49"}]}<p>34.884533408812</p> {"id":131,"nif":"263","name":"Lalo","cognoms":"Lelo","file":"uploads\/count.png","birthday":"0000-00-00","presentacion":"jejjsjsj","email":"vsdfsfaf#aasa.com","password":"$2y$10$QtrolUS9emCS7bfirsly9.JXt9AsYfAc.\/vA0iJCs47\/3g\/ypc8d6","idempresa":1,"id_poblacion":175,"id_online":0,"remember_token":"B1vEqdX8w42i7sLNvG201EArwSTrODe0DDzkVmjzj48ahMMV8oOLmzYRM5Mp","created_at":"2015-05-20 06:52:59","updated_at":"2015-06-06 08:35:13","municipio":{"id":175,"idprovincia":9,"poblacion":"Benej\u00fazar","poblacionseo":"benejuzar","postal":3390,"latitud":"38.083525","longitud":"-0.836494"},"subastas":[{"id":15,"nombre":"Nombre","descripcion":"Descripcion","precio_salida":142,"cant_actual":142,"id_estado":1,"id_metode_envio":1,"id_metodo_pago":1,"id_creador":131,"id_ganador":131,"id_categoria":33,"id_adquirido":1,"data_inici":"2015-06-18","data_final":"2015-06-17","durada":1,"created_at":"2015-06-06 00:00:00","updated_at":"2015-06-06 10:00:57"}]}<p>475.51158492016</p>
When I use the following foreach I show the information
#foreach ($subastas as $subasta)
{{$subasta}}
#endforeach
Now I try to use the following code
#foreach ($subastas as $subasta)
#foreach ($subasta->subastas as $sub)
{{$sub}}
#endforeach
#endforeach
And I recieve this error
Trying to get property of non-object (View: C:\xampp3\htdocs\laravel\resources\views\prodcercanos.blade.php)
Any solution for this problem ?
The question is , how I can render the json in laravel ?
The controller to recieve the JSON
public function productoscercanos ($lat,$long){
$subastas = Subasta::all();
$categorias = Categoria::all();
$provincias = Provincia::all();
$cercanos = User::with(array('municipio', 'subastas'))->get();
$coordA = Geotools::coordinate([$lat,$long]);
$output = [];
foreach ($cercanos as $p) {
if(count($p->subastas)>0){
$coordB = Geotools::coordinate([$p->municipio->latitud,$p->municipio->longitud]);
$distance = Geotools::distance()->setFrom($coordA)->setTo($coordB);
$dis = $distance->in('km')->haversine();
$output[$dis] = $p."<p>$dis</p>";
}
}
ksort($output);
return view('prodcercanos')->with('categorias',$categorias)->with('provincias',$provincias)->with('subastas',$output);
}
$json = (your json file);
$subasta = json_decode($json);
then you should be able to use the json as a object now.
Try array syntax in the second foreach loop.
#foreach ($subasta['subastas'] as $sub)
{{$sub}}
#endforeach
Your code is outputting exactly what is defined. First look at how the controller is building the array.
$output[$dis] = $p."<p>$dis</p>";
$p is a User model from the variable $cercanos, appending it to $dis results in it being converted into JSON and added to your output array which your view iterates and outputs.
It looks like you are trying to add the distance to the user and pass it on.
Instead use an Accessor & Mutator to add an extra attribute to your model then pass on the $cercanos list to the view, from there you can render using a for each and grab the value.
#foreach($cercanos as $c)
{{ $c->username }} {{ $c->dis }}
#endforeach
Update
Once you have added your accessor and mutator you will be able to sort using Collections built in method.
$cercanos->sortBy('dis');

Categories