I have the following code that shows the output of a search for users, groups and posts :
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
How can I hide the <header> if one of them doesn't have results ?
Using eloquent #if and #endif with it s count() function:
#if(count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
#if(count($groups))
<header><h1>Groups results</h1></header>
#foreach ($groups as $group)
#include('group.gruopblock')
#endforeach
#endif
#if(count($posts))
<header><h1>Posts results</h1></header>
#foreach ($posts as $post)
#include('post.postblock')
#endforeach
#endif
#if (count($users))
<header><h1>Users results</h1></header>
#foreach ($users as $user)
#include('user.userblock')
#endforeach
#endif
and so on...
You've got several choices on how to do it thanks to the nature of the Laravel collection. But an if check conditional on either side will work fine:
if (empty($users)) { // Your header and foreach}
if ($users->first()) { // Your header and foreach}
if (!$users->isEmpty()) { // Your header and foreach}
if ($users->count()) { // Your header and foreach}
Related
how to use 2 foreach in laravel?
The banner div cannot be included in the forach. Because the layout is broken.
//for show post 1-5
#foreach ($posts as $post)
//.................
#endforeach
//for banner
<div>
...............
</div
//continue foreach (for show 6-...)
#foreach ($posts as $post)
//.................
#endforeach
You can use loop variable, as like :
//for show post 1-5
#foreach ($posts as $post)
#if($loop->index < 5)
// Your code
#endif
#endforeach
//continue foreach (for show 6-...)
#foreach ($posts as $post)
#if($loop->index > 5)
// Your code
#endif
#endforeach
You can break your posts data into 2 chunks in blade
#foreach($posts->chunk(5) as $chunk)
#foreach($chunk as $post)
//.....
#endforeach
//add your banner here after first chunk is rendered
#endforeach
Or a single loop and check for current iteration
#foreach($posts as $post)
//...........
#if($loop->iteration == 5)
//add your banner here
#endif
#endforeach
I am calling categories collection from controller and displaying in the blade in foreach loops
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="1">{{ $subcategory->name }}</a>
#endforeach
#endforeach
I need to add index numbers generated in the loop from 1 in data-column value
data-column="1"
data-column="2"
data-column="3"
so on....in
For doing this you need to make a variable for counting after that you should pass that variable to view like below.
I am inside a method
public function getSingle($slug){
$category= Post::where('slug','=',$slug)->first();
if ($post != null) {
$counter = 0;
return view('blog.single')->withCategories($category)->withCounter($counter);
} else {
return view('error.error404');
}
}
After that you should access that Counter variable in view like below
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{$counter++}}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
The classic for will do
#foreach ($categories as $category)
#for ($i = 0; $i < count($category->subcategories); $i++)
<a class="a.toggle-vis" data-column="{{$i}}">{{ $category->subcategories[$i]->name }}</a>
#endfor
#endforeach
For a shorter one, I think, we can do something like this. A little bit dirty in view but in the small snippet, it should be okay.
{{ !($index = 1) }}
#foreach ($categories as $category)
#foreach ($category->subcategories as $subcategory)
<a class="a.toggle-vis" data-column="{{ $index++ }}">{{ $subcategory->name }}</a>
#endforeach
#endforeach
Use this simple solution (Laravel method):
$loop->iteration The current loop iteration (starts at 1).
It will automatically increment, in every loop iteration.
Check docs:
The Loop Variable
This is how I wish to be
Actual
Status gets printed 3 times
I return this objects from my controller:
return view('ViewTicket') ->with('tickets', $tickets)
->with('user', $user)
->with('priority', $priority)
->with('status', $status)
->with('type', $type);
However I want to print the respective fields like in my view:
#foreach ($tickets as $t)
<tr>
<td> {{$t->id}} </td>
#foreach ($user as $u)
#if($t->user_id==$u->Id)
<td>{{ $u->UserName }}</td>
#endif
#endforeach
Even this doesnt solve my problem.Is there any way to avoid the loop inside the loop to get these data?The goal is to get respective fields for each ticket
If I dd($user) it returns 3 values okay
when I loop in my view it displays 9 values,which means it loops 3 times the lements
Thanks in Advance
ASSUMPTIONS
$tickets is a collection of ticket objects.
$user is a collection of user objects.
CODE
#foreach ($tickets as $t)
<tr>
#foreach ($user as $u)
#if($t->user_id == $u->Id)
<td> {{$t->id}} </td>
<td>{{ $u->UserName }}</td>
#endif
#endforeach
</tr>
#endforeach
I have a controller which gets an array of a User's diaries from my database and passes them to my view:
<?php
public function readDiaries($hash)
{
$user = User::where('hash', $hash)->first();
$diaries = Diary::where('user_id', $user->id)->get();
return view('app.diary.readDiaries', ['diaries' => $diaries]);
}
In my view, I am looping through the diaries using a #foreach loop.
<div id="diaries" class="card-columns">
#if (count($diaries) > 0)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
#endif
</div>
But I am getting the following undefined variable error...
Undefined variable: diary (View: C:\xampp\htdocs\personal_projects\Active\diary_app\resources\views\app\diary\readDiaries.blade.php)
Why is my $diary variable undefined inside the #foreach loop?
You have a typo
change #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
and it should work!
There is some typo in var_dump use {{ var_dump($dairy) }}
Try to use compact method for pass data to view as shown below
//return view('app.diary.readDiaries', compact('diaries'));
public function readDiaries($hash)
{
$user = User::where('hash', $hash)
->first();
$diaries = Diary::where('user_id', $user->id)
->get();
return view('app.diary.readDiaries', compact('diaries'));
}
It is just a simple spelling mistake,
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
in your foreach you are passing $dairy and dumping var name is $diary
chane it to
#foreach ($diaries as $dairy)
{{ var_dump($dairy) }}
#endforeach
Habbit of copy paste is sometimes good for us...
:)
#foreach ($diaries as $dairy)
{{ var_dump($diary) }}
#endforeach
you used ($diaries as $dairy) in foreach
but in foreach you used ($diary)
you want edit this {{ var_dump($diary) }} to {{ var_dump($dairy) }}
or you want edit #foreach ($diaries as $dairy) to #foreach ($diaries as $diary)
I am not very experienced and I run into problem. I can't figure out how to make this function use chunks. Tried all but get errors, I think I am misunderstanding something.
$companies_has = Company::whereHas('websites', function ($q) {
$q->where( 'updated_at', '<=', Carbon::now()->subMonth());
})->get();
$companies_doesnt_has = Company::whereDoesntHave('websites')->get();
$companies = $companies_has->merge($companies_doesnt_has);
foreach ($companies as $company) {
$this->getWebsite($company); }
#foreach ($companies->chunk(3) as $chunk)
<div class="row">
#foreach ($chunk as $product)
<div class="col-xs-4">{{ $product->name }}</div>
#endforeach
</div>
#endforeach