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
i'm trying to get all the Level_id and Term_id on my intermediate table using many to many relationship but i couldn't make it work and i want to pass it to my view using ordered list html.
i always end up with this..Trying to get property of non-object
Controller:
public function get_term_level()
{
$terms=Term::find(1);
foreach ($terms->level as $tm) {
$tm->pivot->Level_id;
}
return view('term_level.index',compact('terms'));
}
Term Model same as with my LevelModel
public function level(){
return $this->belongsToMany(Level::class, 'term_levels')->withPivot('Term_id', 'Level_id');
}
View
#foreach ($terms->pivot as $tm )
<ul>
<li>{{ $tm->Term_id }}</li>
{{ $tm->Level_id }}
</ul>#endforeach
Try this (inside your controller):
foreach ($terms->level()->get() as $tm) {
$tm->pivot->Level_id;
}
Instead of creating a foreach loop I want to iterate over the results and
I am using the each() method:
$collection = Comment::all();
$comment = $collection->each(function ($comment){
dd($comment->comment);
});
when I dd() I get:
"Hatter; 'so I should think."
But when I pass to view:
return view('welcome')->with('comment',$comment);
I get
[{"id":1,"post_id":5,"comment":"Hatter; 'so I should think.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},
{"id":2,"post_id":5,"comment":"Alice gently remarked;.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},
and so on..
This is the view:
{{$comment}}
I want to iterate through the collection and put data into $comment and then show it in the view.
It is quite simple like this.
Iterate then -> following the property name
#foreach($comment as $row)
<li>{{ $row->id }}</li>
<li>{{ $row->post_id}}</li>
//AND SO ON
#endforeach
I'm having trouble outputting a list of key value pairs in Laravel blade.
My show.blade.php template is:
#extends('layouts.app')
#section('content')
<ul>
#foreach ($datapoint as $key => $value)
<li>{{ $key, $value }}</li>
#endforeach
</ul>
{{ $datapoint }}
#endsection
And my controller which displays this template is:
<?php
namespace App\Http\Controllers;
use App\Hyfes;
use Illuminate\Http\Request;
class CaptainController extends Controller
{
public function getLiveData ()
{
$datapoint = Hyfes::find(1);
return view('captain.show')->with('datapoint', $datapoint);
}
}
The data output to the view in the browser is this:
incrementing
exists
wasRecentlyCreated
timestamps
{"date":"08/07/2017","time":"12:02:25","boat_name":"cyclone","current_location":"North Greenwich Pier","status":"docked","embarked":26,"disembarked":2,"people_on_board":24,"current_speed":0,"hotel_power":231,"battery_power_demand":342,"battery_output_power":23,"engine_output_power":12,"battery_power_stored":100,"battery_depth_of_discharge":323,"fuel_consumption_rate":7,"nox":432,"co2":213,"battery_state_of_charge":100,"id":1}
Ideally, I would like to see a list of bullet points showing e.g.:
date: 8/07/2017
time: 12:00:01
boat_name: cyclone
The function Hyfes::find(1) will return an object of the type App\Hyfes and not an array. By looping over the key value pairs you are getting all properties. Laravel adds a lot of extra properties for keeping track of the object.
A better way to do this is using the Laravel getAttributes function. This will only get the attributes loaded from the database.
Like so:
#foreach ($datapoint->getAttributes() as $key => $value)
<li>{{ $key }}: {{ $value }}</li>
#endforeach
You are trying to iterate over a non array variable in your template. You have to be explicit about the values of the objects you would like to see in your template.
#extends('layouts.app')
#section('content')
<ul>
<li>date: {{ $datapoint->date }}</li>
<li>time: {{ $datapoint->time }}</li>
<!-- and so on -->
</ul>
#endsection
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');