How to figure the question into 100 in relationship in laravel - php

I have two models 1. Subject 2. Question and it has One to many relation. And a student can choose many subjects he want. And now I want to take a test through the subjects. And the test should be 100 marks test.Now i need a way how to accomplish the 100 marks test. Suppose a student choose 3 subjects and if we divide 3 form 100 then it will be 33 (floor) or 34 (ceil) per subject but i want to round it to 100, how i accomplish this. Here is my code for getting the questionsn
foreach ($student->departments as $key => $department){
$majorSubjects[] =$department->subject_id;
}
$no_of_questions =100;
$uniqueSubjects=array_unique($majorSubjects);
$div = ceil($no_of_questions/count($uniqueSubjects));
$mul = $div*count($uniqueSubjects);
$subjects=Subject::whereIn('id',$majorSubjects)->get();
}
and in my blade
#foreach($subjects as $key => $subject)
<li class=" {{$key == 0 ? 'active' : ''}}">{{$subject->name}}</li>
#endforeach
</ul>
<div class="tab-content">
#if(!empty($subjects))
#foreach($subjects as $key => $subject)
<div class="tab-pane {{$key == 0 ? 'active' : ''}}" id="tab_{{ $subject->id }}">
#foreach($subject->questions->random($div) as $num => $question)
<form></form>
#endforeach

You can use modulus to get the remaining mark test.
$additional_mark_test = $no_of_questions % count($uniqueSubjects);
Put it in here.
foreach ($student->departments as $key => $department){
$majorSubjects[] =$department->subject_id;
}
$no_of_questions =100;
$uniqueSubjects=array_unique($majorSubjects);
$div = ceil($no_of_questions/count($uniqueSubjects));
$mul = $div*count($uniqueSubjects);
// Get the remaining test
$additional_mark_test = $no_of_questions % count($uniqueSubjects);
$subjects=Subject::whereIn('id',$majorSubjects)->get();
}
In your blade, this will check first if $additional_mark_test is empty.
#if(!empty($additional_mark_test))
#foreach($subject->questions->random($additional_mark_test) as $num => $question)
<form></form>
#endforeach
#php $additional_mark_test = 0; #endphp
#endif
Put it in here.
#foreach($subjects as $key => $subject)
<li class=" {{$key == 0 ? 'active' : ''}}">{{$subject->name}}</li>
#endforeach
</ul>
<div class="tab-content">
#if(!empty($subjects))
#foreach($subjects as $key => $subject)
<div class="tab-pane {{$key == 0 ? 'active' : ''}}" id="tab_{{ $subject->id }}">
#foreach($subject->questions->random($div) as $num => $question)
<form></form>
#endforeach
<!--Additional Mark Test(This will be add to first subject)-->
#if(!empty($additional_mark_test))
#foreach($subject->questions->random($additional_mark_test) as $num => $question)
<form></form>
#endforeach
#php $additional_mark_test = 0; #endphp
#endif

Related

I want to get value or zero for display if id has a record in Laravel?

This is about Laravel query. I want get some data from db. I used below code for it,
$ids = [1, 2, 3, 4, 5]
$stock = $item->stocks->whereIn('paperorder_paper_id', $ids)->where('status', 'qc-pass')->get();
When simplified My problem is if id no 1 has a record it should display that value otherwise should display zero ("0"). Because I want to put data in to datable. Please see the image. Then clearly understand what is my problem.
Expected:
Problem and I got the result
stocks DB
Id Value
1
2 120
3 50
4
5 1000
below has my real code in the blade file. But complicated to explain using real code. Please help me solved this thing.
<td class="border-t border-gray-200 border-dashed">
#php
$paperorderids = [];
$paperorder = $item->paperorders->where('paperorder_id', $item->id);
foreach ($paperorder as $key => $value) {
$paperorderids[] = $value->id;
}
// Output: $ids = [1, 2, 3, 4, 5]
$stock = $item->stocks->whereIn('paperorder_papers_id', $paperorderids)->where('status', 'qc-pass');
// Output: 2 arrays only becasue others id has not record thats why I want zero for that.
foreach ($stock as $key => $val) {
echo $val->qty ? $val->qty : 0;
}
//
#endphp
{{-- #foreach ($item->paperorders as $paperorder)
#foreach ($item->stocks as $stock)
#if ($paperorder->id == $stock->paperorder_papers_id && $stock->status === 'qc-pass')
<span class="flex items-center px-6 py-0 text-gray-700">
{{ $stock->qty }}
</span>
#else
<span class="flex items-center px-6 py-0 text-gray-700">
0
</span>
#endif
#endforeach
#endforeach --}}
</td>
That should do it.
echo in_array($val->id, $ids) ? $val->qty : 0;
Thanks for help me here is my final code and it's worked for me.
#php
$stock = $item->stocks->where('status', 'qc-pass');
$stockids = [];
foreach ($stock as $key => $sids) { $stockids[] = $sids->paperorder_papers_id;}
#endphp
#foreach ($item->paperorders as $val)
<span class="flex items-center px-6 py-0 text-gray-700">
#if (in_array($val->id, $stockids))
{{ $item->stocks->where('paperorder_papers_id', $val->id)->where('status', 'qc-pass')->sum('qty'); }}
#else
0
#endif
</span>
#endforeach
Result

foreach in foreach in if blade php

#foreach ($capacityGroups as $capacityGroupFather)
#foreach ($exampleProjects as $k => $exampleProject)
#foreach ($exampleProject->capacityGroups as $j => $exampleCapacity)
#if ($exampleCapacity->id == $capacityGroupFather->id)
Only the first result of the loop
<div class="carousel-item active">
The rest of the loop
<div class="carousel-item">
We have the example Projects collection, each of which has capacityGroups.
I need to bring all the example Projects for each capacity Group. But the first of each goes into a special div (carousel-item active)
and the rest of each goes into (carousel-item) how can I fix this problem
Simply use the loop variable.
#foreach ($exampleProjects as $k => $exampleProject)
<div class="carousel-item{{ $loop->first ? ' active' : '' }}">
</div>
#endforeach

setting variables inside a Laravel foreach loop

I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}

Laravel - Collections. How to separate array into 2 groups

I have created 2 collections(arrays) which each contain an array of 5 items/events.
$events = App\get_event_data( $args )
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#foreach ($event_chunks as $chunk)
Output of the above:
object(Illuminate\Support\Collection)[27632]
protected 'items' =>
array (size=2)
0 =>
object(Illuminate\Support\Collection)[27630]
protected 'items' =>
array (size=5)
...
1 =>
object(Illuminate\Support\Collection)[27631]
protected 'items' =>
array (size=5)
...
In my next loop, it simply goes through every item of the array 1 by 1.
I need to split the 5 items into a further 2 groups:
group of 2
group of 3
so I can wrap a div around each group.
Current loop:
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
What I need:
<div class="item-group">
[item1, item2]
</div>
<div class="item-group">
[item3, item4, item5]
</div>
Full code:
{{-- Get all events --}}
#if( $events = App\get_event_data( $args ) )
#php
$collection = collect($events['events']);
$event_chunks = $collection->chunk(5);
$event_chunks->toArray();
#endphp
#foreach ($event_chunks as $chunk)
<div class="{{ $block }}__flex-grid">
#foreach ($chunk as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
#endif
Assuming that you will allways have 5 items, a solution could be:
<div class="item-group">
#foreach (array_slice($chunk->toArray(),0,2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
<div class="item-group">
#foreach (array_slice($chunk->toArray(),2) as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
Or if want to avoid code duplication:
#php $chunk = [array_slice($chunk->toArray(),0,2), array_slice($chunk,2)];
#foreach ($chunk as $group)
<div class="item-group">
#foreach ($group as $key => $event)
<div class="item">
{{ $event['title'] }}
</div>
#endforeach
</div>
#endforeach
If you don't know or not sure that you have 5, you may need to change the logic of the chunks/slice.
It's maybe too late to answer, but I've made a function to split arrays into as groups as you like.
function SplitInto($array, $groups = 2)
{
$members = count($array) / $groups;
if (count($array) % $groups) $members = (int)(count($array) / $groups) + 1;
return array_chunk($array, $members);
}

How to print only 4 values inside foreach loop?

There may be 5 or 6 values inside the foreach loop but i need to print suppose first 5 or 6 values.How do i do that?
<div class="tag-area">
#foreach(explode(',',$product->tags) as $tag)
<span>{{$tag}}</span>
#endforeach
</div>
You should try this:
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#endif
#endforeach
</div>
This will help you.
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#endif
#endforeach
</div>
If your key is numenric and its of indexed array you can directly do it like:
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key => $tag)
#if($key <= 5)
<span>{{$tag}}</span>
#else
<?php break; ?>
#endif
#endforeach
OR try this;
<div class="tag-area">
<?php $cnt == 0; ?>
#foreach(explode(',',$product->tags) as $tag)
<span>{{$tag}}</span>
<?php
$cnt++;
if($cnt >= 5)
break;
?>
#endforeach
Remember break; will stop unnecessary execution of loop
if you have 10 element in array no need to iterate after 4 iteration so you should break foreach iteration
<div class="tag-area">
#foreach(explode(',',$product->tags) as $key=>$tag)
#if($key >= 4)
#break
#endif
<span>{{$tag}}</span>
#endforeach
</div>

Categories