'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
Related
my controller
public function lemari_surat_alpha(){
$show_dinas = dinas::all();
$show_surat = surats::all();
return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));
}
I don't know to show many data with array for selecting in Model
You're basically trying to group the results in alphabetical order within view. Why not arrange your results first and then display it.
Try this.
public function lemari_surat_alpha(){
$show_dinas = dinas::all();
// Uppercase alphabet keys collection
$alphabets = collect(range('A', 'Z'))->flip();
// Grouped results
$results = surats::all()->groupBy(function($item) {
return strtoupper($item->nama_surat[0]);
});
// Merge results with alphabets
$show_surat = $alphabets->map(function($item, $key) use ($results) {
return $results->has($key) ? $results->get($key) : null;
});
return view('frontend.daftar_surat_perdinas', compact('show_dinas','show_surat'));
}
This will return grouped results. Like this
then you can iterate over the collection in your blade file like this
#foreach ($show_surat as $alphabet => $surats)
<div class="entry-image">
<a href="#" class="entry-link" target="_blank">
{{ $alphabet }}
</a>
</div>
#if (is_null($surats))
No Results
#else
<ul style="margin-left: 20px;">
#foreach($surats as $surat)
<li style="font-size: 18px;">{{ $surat->nama_surat }}</li>
#endforeach
</ul>
#endif
#endforeach
VIEW:
#foreach( $show_dinas as $show_dina){
//do something
#endforeach
I am getting results from a database in a controller and storing them as objects in an array
$roleSentinel = Sentinel::findRoleByName($role);
$permisssionsName = array_keys($roleSentinel['permissions']);
$permissions = array();
foreach ($permisssionsName as $permiso) {
$permissions[] = DB::table('sec_permission')->where('name', '=',$permiso)->get();
}
return view('menu', ['role' => $role,'permisos' => $permisos]);
an object would be like
[{"permission_id":1,"name":"foo","object_id":1,"operation_id":1}]
So then in the view I am trying to get the values and printing them
<b>{{$role}}</b><br>
#foreach ($permisos as $permiso)
#foreach ($permiso->name as $name)
{{$name}}
#endforeach
#endforeach
however I am getting Undefined property: Illuminate\Support\Collection::$name
Each variable in the array is an array of objects. There's two ways you can go about it.
Add another foreach:
#foreach ($permisos as $permiso)
#foreach ($permiso as $perm)
#foreach ($perm->name as $name)
{{$name}}
#endforeach
#endforeach
#endforeach
Or get one full collection with all of the names:
$permissionsName = array_keys($roleSentinel['permissions']);
$permissions = DB::table('sec_permission')->whereIn('name',$permissionsName)->get();
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.
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.