dd() reads values that var_dump() can't read - php

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.

Related

Add elements to an array inside a foreach loop

'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

Laravel : Explode array with relation belongsTo to model

I want to explode array value and have successfully doing so by using this code :
#foreach(explode('.', $comment->topic_id) as $topic)
{{ $topic }}
#endforeach
This is the output
Topic : 1,2
The problem is, I want to implement relation belongsTo to the topic_id. When I add the relation and run the code, unfortunately only one of the value is shown.
#foreach(explode('.', $comment->getTopic->topic) as $topic)
{{ $topic }}
#endforeach
This is my model
public function getTopic()
{
return $this->belongsTo('App\Topic', 'topic_id', 'id');
}
Output :
Topic : Laravel
What is the right way to call this array? Please help me. Thank you.
You can't use relationship in this case. If you simply looking for solution then you could do something like the following:
<?php $topic_ids = explode('.', $comment->topic_id);
$topics = App\Topic::whereIn(id, $topic_ids)->get();
?>
#foreach($topics as $topic)
{{ $topic }}
#endforeach
BTW you should structure your database more efficiently.
#foreach(explode(',', $item->chapter_id) as $layer)
<?php
$chapters = DB::table('topic_types')->where('id', $layer)->get();
?>
#foreach ($chapters as $ch)
{{$ch->name}} <br>
#endforeach
#endforeach

Unique collection in Laravel

I need to find out unique value. So I tried bellow code. It is through undefined variable error.
Controller:
$employee = Employee::all();
Return view ('page', compact('employee'));
Page view:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
//Display Entire data
#foreach($employee as #employee)
//Display all value
#endforeach
But I got an uniqueEmpLoc is undefined error. I'm using LARAVEL 5.1. Please help me to solve this problem.
There are some errors in your code:
I don't think compact(employee) would work. Shouldn't that suppose to be compact('employee') ?
In Blade, there is no need of putting curly braces at all. Remove them.
Try out the following:
$employees = Employee::unique('locations')->values()->list('location')->toArray();
return view('page', compact('employees'));
And then in your view:
#foreach($employees as $employee)
{{ $employee }}
#endforeach
Use this query in controller
$employees = Employee::distinct()->list('location')->toArray();
return view('page', compact('employees'));
In view
#foreach($employees as $employee)
{{ $employee }}
#endforeach
I agree with the other answers here, this code should be in the controller. You shouldn't be doing logic in the views.
In the controller do:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
$employee = Employee::all();
Return view ('page', compact('employee', 'uniqueEmpLoc'));
The reason your code isn't working is the line that defines $uniqueEmpLoc is interpreted by blade as text, not code.
If you really want to do that in the view, you need to wrap it in #php tags.
#php
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#endphp
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach

Laravel Count with where inside blade

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.

Pass an object to a view from controller

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.

Categories