Hi everybody here I have 2 paths so I return 2 arrays of tasks I need to count each array So in last I get
[6, 4]
#foreach ($path->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->id,$a))
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endif
#endforeach
#endforeach
Easiest php way of counting arrays is count($array) so if you want to make a new array with the 2 array counts you can do it like this:
$counts = [count($array1), count($array2)];
But if you need to count the records in the database, you will need to change the query instead of ->get() you will need to use ->count().
You should try this:
$path = count($path->pathtags);
$task = count($Tag->Tasks);
Updated answer
$path = count($path->pathtags);
#foreach ($path->pathtags as $Tag)
$task = count($Tag->Tasks);
#foreach ($Tag->Tasks as $Task)
<li class="list-group-item"> Task : {{ $Task->task_name }} </li>
#endforeach
#endforeach
Related
I have relation tags that have many tasks whet I get data from foreach I get duplicated values
in example
in my path i have 2 tags PHP ,HTML5
PHP Has [PHP Task_1 , PHP_Task_2]
HTML5 Has [HTml5 Task_1,PHPTask_2]
because task has many tags so i will get to duplicated PHP_Task_2
i need to get every task just one time
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)
<li> Path Tag :: {{ $Tag->name }} </li>
#foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
#foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
#endforeach
#endforeach
#endforeach
#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
I had an for loop inside that will display an indexed array from the results column that saves an array in a DB, inside a forelse loop.
This is how I store the array
$final_result = ResultRecordFile::create([
'request_id' => $request->id,
'date_released' => $date,
'lab_access_code' => $input['lab_access_code'],
'remarks' => $input['remarks'],
'results' => json_encode($input['results']) // Stores array in the column
]);
Now I will retrieve all these data in my view using blade.
The array from the db is exactly like this when I use json_decode for the results field
[
"Result1",
"Result2",
]
This is what I have done so far.
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#foreach(json_decode($result->results) as $value)
{{ $value }}
#endforeach
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
But it returns me an output of this in the view.
Test result for this sub method result is Result1 Result2
Test result for this sub method result is Result1 Result2
I have also tried this code below:
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#for($i=0; $i < count($result->results); $i++)
{{ $result->results[$i]}}
#endfor
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
But now it returns me this in my view
Test result for this sub method result is [
Test result for this sub method result is [
The output is supposed to be like these:
Test result for this sub method result is Result1
Test result for this sub method result is Result2
The problem here is that it returns me the whole value of the array that it should return each value of the array stored in the database.
Appreciate if someone could help.
Thanks in advance.
This might work for you
$result_datas = json_decode($result->results);
$index = 0;
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
<li>
<b>{{ $submethod->name }}</b> result is
#if(isset($result_datas[$index]))
{{ $result_datas[$index] }}
#endif
<?php $index++;?>
</li>
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
Try this:
#forelse ($result->request->methodology->submethods as $submethod)
<ul>
#foreach(json_decode($result->results) as $value)
<li>
<b>{{ $submethod->name }}</b> result is
{{ $value }}
</li>
#endforeach
</ul>
#empty
<p>This request has no submethods</p>
#endforelse
I try like this :
<div class="media-body">
#foreach($categories as $category)
#php $category[] = $category->name #endphp
#endforeach
{{ implode(",", $category) }}
</div>
If the code executed, there exist error :
undefine variable category
How can I solve it?
You can simply use Laravel Collection
{{ $categories->pluck('name')->implode(', ') }}
Or if you wanna do this in foreach then
#php ($names = [])
#foreach ($categories as $category)
#php ($names[] = $category->name)
#endforeach
{{ implode(', ', $names) }}
You have to declare an array within <?php ... ?> block and then use the same in a {{blade}} block.
I have a #foreach loop in the Blade template and need to apply special formatting to the first item in the collection. How do I add a conditional to check if this is the first item?
#foreach($items as $item)
<h4>{{ $item->program_name }}</h4>
#endforeach`
Laravel 5.3 provides a $loop variable in foreach loops.
#foreach ($users as $user)
#if ($loop->first)
This is the first iteration.
#endif
#if ($loop->last)
This is the last iteration.
#endif
<p>This is user {{ $user->id }}</p>
#endforeach
Docs: https://laravel.com/docs/5.3/blade#the-loop-variable
SoHo,
The quickest way is to compare the current element with the first element in the array:
#foreach($items as $item)
#if ($item == reset($items )) First Item: #endif
<h4>{{ $item->program_name }}</h4>
#endforeach
Or otherwise, if it's not an associative array, you could check the index value as per the answer above - but that wouldn't work if the array is associative.
Just take the key value
#foreach($items as $index => $item)
#if($index == 0)
...
#endif
<h4>{{ $item->program_name }}</h4>
#endforeach
As of Laravel 7.25, Blade now includes a new #once component, so you can do it like this:
#foreach($items as $item)
#once
<h4>{{ $item->program_name }}</h4> // Displayed only once
#endonce
// ... rest of looped output
#endforeach
Laravel 7.* provides a first() helper function.
{{ $items->first()->program_name }}
*Note that I'm not sure when this was introduced. So, it may not work on earlier versions.
It is only briefly mentioned in the documentation here.
The major problem with Liam Wiltshire's answer is the performance because:
reset($items) rewind the pointer of $items collection again and again at each loop... always with then same result.
Both $item and the result of reset($item) are objects, so $item == reset($items) requires a full comparison of its attributes... demanding more processor time.
A more efficient and elegant way to do that -as Shannon suggests- is to use the Blade's $loop variable:
#foreach($items as $item)
#if ($loop->first) First Item: #endif
<h4>{{ $item->program_name }}</h4>
#endforeach
If you want to apply a special format to the first element, then maybe you could do something like (using the ternary conditional operator ?: ):
#foreach($items as $item)
<h4 {!! $loop->first ? 'class="special"': '' !!}>{{ $item->program_name }}</h4>
#endforeach
Note the use of {!! and !!} tags instead of {{ }} notation to avoid html encoding of the double quotes around of special string.
Regards.
if you need only the first element you can use #break inside your #foreach or #if.see example:
#foreach($media as $m)
#if ($m->title == $loc->title) :
<img class="card-img-top img-fluid" src="images/{{ $m->img }}">
#break
#endif
#endforeach
you can do it by this way.
collect($users )->first();
To get the first element of a collection in Laravel, you can use :
#foreach($items as $item)
#if($item == $items->first()) {{-- first item --}}
<h4>{{$item->program_name}}</h4>
#else
<h5>{{$item->program_name}}</h5>
#endif
#endforeach