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
Related
Basically I have a function within a controller which is
public function get_all_clubs($city)
{
$city = ucwords($city);
$city_des = Town::where('town', $city)->value('town_desc');
$clubs = Club::get()->where('city', $city)->where('profile_enabled', '1');
$events = Event::where('club_id', $clubs->first()->id)->get();
$lowercase_city = Str::lower($city);
return view('frontend.pages.allclubs', compact('clubs', 'lowercase_city', 'city_des', 'events', 'flag'));
}
The events and the club tables are related using an id as you might have figured out. I have been able to get the events associated to a city based on the club_id. In my view I am echoing each event using the
#foreach Laravel syntax. The problem however is I need to create an hyperlink for each event on on my view. The routes are structred in the way below:
<a href="{{ route('event.show_event', [$club->slug, $event->slug]) }}">
Effectively, I need to pass the $event->slug which I already have and also the $club->slug which is what my question is, how do I get hold of this variable during my #foreach loop. Please note I am an intermediate Laravel/php developer.
My view is as follows
#foreach ($events as $event)
#if ( ( (strtotime((str_replace('/', '-', $event->date)))) >= (strtotime("today")) ) && ($event->event_private))
#php $flag='1'; #endphp
<li>
<div class="widget-posts-body">
<h6 class="widget-posts-title"> {{$event->title}} </h6>
</div>
</li>
#endif
#if ( ($loop->index) == '10' )
#break
#endif
#endforeach
#if($flag != '1')
<li>
<p> There are no events live at this time. Please check back later</p>
</li>
#endif
make a one to one relation in event model to club like this:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function club()
{
return $this->hasOne(Club::class);
}
then call it in view like this:
<a href="{{ route('event.show_event', [$event->club->slug, $event->slug]) }}">
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 have a Laravel model with many attributes. So, I need iterate over these attributes. How can I do this?
Something like this:
#foreach($model->attributes as $attribute)
// use $attribute
#endforeach
is it possible?
If you have an object, use getAttributes():
#foreach($model->getAttributes() as $key => $value)
// use $attribute
#endforeach
If these attributes were returned from a query, you can access the Model class directly:
E.g.
#foreach (User::all() as $user)
<p>This is user {{ $user->id }}</p>
#endforeach
or, you can access the model reference given to a template:
E.g.
#foreach ($user->permissions as $permission)
<p>This is an user permission {{ $permission->id }}</p>
#endforeach
See more in the Laravel Docs
I am trying to show my variables values to view page, but it's showing the error of trying to get property of non-object. Please someone help me.
Here is my Controller-
public function myAffiliates(Request $request) {
if ($user_id = Sentinel::getUser()->id) {
$affiliates = DB::table('affiliate_users')
->where('affiliate_users.user_id', $user_id)
->first();
}
//dd($affiliates);
return view('dashboard.affiliates.show',['affiliates' => $affiliates]);
}
And I wrote to my view page like this-
#foreach ($affiliates as $affiliate)
<ul>
<li>First Upline - {{ $affiliate->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliate->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliate->third_upline_id }} Pts.</li>
</ul>
#endforeach
use first() to retrieve a single object
use get() to retrieve collection and iterate it on your blade
You are trying to iterate on object's properties try this
<ul>
<li>First Upline - {{ $affiliates->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliates->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliates->third_upline_id }} Pts.</li>
</ul>
I am trying to pass a set of rows from a controller to a view:
$items = Items::where('group', '=', $group);
return view('listing', [
"group" => $group,
"exists" => $items->exists(),
"items" => $items->get(),
]);
And in my view:
#if (!$exists)
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
The view will only return 1 item though. The length of $items is 1. If I count($items) in the controller, I get the expected number of items.
How can I pass an object to my view from a controller?
My final solution:
Controller
$items = Items::where('group', '=', $group);
return view('listing', [
"group" => $group,
"items" => $items->get(),
"exists" => $items->exists(), // Important! Has to go after!
]);
And in my view:
#if (!$exists)
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
The issue is the call to exists() before the call to get(). The call to exists() is modifying your query builder to add a limit(1) to it. Therefore, when you call get() after exists(), the query builder still has the limit(1) attached.
Your updated solution works because you removed the call to exists().
However, the call to get() should still be done in the Controller. The view should only be passed the collection of objects, not the query builder.
I believe if you put get() at the end it will work.
$items = Items::where('group', '=', $group)->get();
You are just pulling back the model object and not the data. That is why the count is 1 when you are expecting 4.
Edited per comment
I think the get where you have it might be causing some funkiness.
$items = Items::where('group', '=', $group)->get();
return view('listing', [
"group" => $group,
"items" => $items,
]);
#if (!$items->exists())
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
You might put a dd($items->toArray()); after you query Items to see what you get.