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.
Related
'm trying to get non duplicated in My blade tasks using this Code
I have Edited My Question So i added Controller and full blade Code
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#php $a=array(); #endphp
#if (in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,"$Task->task_name");
#endphp
#else {
<li> Task :: Not Found </li>
}
#endif
#endforeach
#endforeach
#endforeach
you are emptying the array in each iteration. move the initialization of array before the foreach loop. also, the whole logic is wrong. you are checking if the item exists in the array, and if so, add it again.
#php $a=array(); #endphp
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,$Task->task_name);
#endphp
#else
<li> Task :: Duplicated </li>
#endif
#endforeach
Change this
array_push($a,"$Task->task_name");
with
array_push($a, $Task->task_name);
// or
$a[] = $Task->task_name;
In stock PHP you can use array_count_values to get the count of each array items.
Then use array_diff or array_intersect to get the different unique or duplicated items.
Array_keys return the values from original array.
$arr = ["one", "one", "two", "two", "three"];
$count = array_count_values($arr);
echo "duplicates \n";
var_dump(array_keys(array_diff($count, [1])));
echo "uniques \n";
var_dump(array_keys(array_intersect($count, [1])));
https://3v4l.org/DKIbZ
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'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
In one of my controllers I call a view with the variable $members:
$members = Member::orderBy("created_at", "desc")->get();
Then in the view I loop over all the member's groups:
#foreach ($members as $group)
<?php $group = $group->group(); ?>
<b>{{ $group->title }}</b> // fails
#endforeach
This throws a Trying to get property of non-object exception. If I do
#foreach ($member as $group)
<?php $group = $group->group(); ?>
<?php dd($group); ?> // works
<b>{{ $group->title }}</b> // fails
#endforeach
I can see all the attributes array populated with all the values I want. Why can dd() read values I can't read using {{ $var }}?
#foreach ($member as $group)
<?php $group = $group->group(); ?>
<?php var_dump($group); ?> // fails
<b>{{ $group->title }}</b> // fails
#endforeach
fails as well.
I think I found the error. For some reason I cannot read $group->value, but $group["value"] works. It may be that group is a keyword.
I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.