« first day (131 days earlier)      last day (217 days later) » 

6:55 AM
Hi sir @ZakariaAcharki
 
 
1 hour later…
8:16 AM
Hi
 
How are you sir ?
 
fine bro
and you ?
 
Alhamdulillah
Good
Sir I explained the issue above
 
show me the output of
Report::where([['class_id', '=', $report->class_id], ['report_categories_id', '=', $report->report_categories_id],['subject', '=', $report->subject]])->pluck('obtained_marks')->toArray();
 
ok sir
array:2 [▼
0 => {#432 ▼
+"obtained_marks": "100"
}
1 => {#430 ▼
+"obtained_marks": "99"
}
]
 
8:30 AM
try
$obtained_marks = Report::where([['class_id', '=', $report->class_id], ['report_categories_id', '=', $report->report_categories_id],['subject', '=', $report->subject]])->pluck('obtained_marks')->toArray();

$json = json_encode($obtained_marks);
$obtained_marks = json_decode($json, true);

dd( $obtained_marks );
 
ok sir
array:2 [▼
0 => "100"
1 => "99"
]
 
I think we are close now
 
now we'll do the same thing here
we'll pass the unique
 
ok sir
 
8:32 AM
sort and reverse
 
Yeah
 
$unique_marks = array_unique( $obtained_marks );
sort($unique_marks);
$obtained_marks = array_reverse($unique_marks);
dd( $obtained_marks );
 
array:2 [▼
0 => "56"
1 => "45"
]
I changed the records
 
That's why its different now
 
8:36 AM
so we're done
 
array_search($input['obt_marks'][$i], $marks)+1
Here in the array search
 
array_search( here_im_not_sure, $obtained_marks)+1
 
I don't have that index $i
ok sir
I will try
 
do you update
the records one by one ?
by id
or you get a list of ids ?
 
one by one
 
8:39 AM
so show me the full update action
to see how you get the selected record info
 
public function update(Request $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();

$input['total_marks'] = $request->total_marks;
$input['obtained_marks'] = $request->obtained_marks;
$input['percentage'] = $request->percentage;
$input['report_categories_name'] = ReportCategories::find( $input['report_categories_id'] )->name;

if (is_numeric($input['obtained_marks'])) {
$percentage = (($input['obtained_marks']/$input['total_marks']) * 100);
} else {
$percentage = 0;
}
Now let me explain its output I just tested now
I have those two records I showed while ago
Now when I try to increase the marks of the record having second position
than the first position marks
Then that updated to first position
That's fine
But the other one is still 1 position
Then it needs to be on the 2nd position
because we updated the whole positions
 
so
we need to update all the records position
not just the selectd one
 
Yeah
That's why I try to loop through all the records in the previous version
 
ok
I guess
those lines
we're not in need of them
 
oh
fine
 
8:49 AM
I guess we need something like
public function update(Request $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();

$input['report_categories_name'] = ReportCategories::find( $input['report_categories_id'] )->name;

if (is_numeric($input['obtained_marks'])) {
$percentage = (($input['obtained_marks']/$input['total_marks']) * 100);
} else {
$percentage = 0;
}

$input['percentage'] = $percentage;

if ($percentage >= 0 && $percentage <= 49) {
$grade = 'D';
} elseif ($percentage >= 50 && $percentage <= 59) {
 
ok sir
I am trying it
It updated one record in a write way
And now its not updating anything
And have no errors too
foreach( $all_reports->get() as $rep) {
$position = array_search( $rep['obtained_marks'], $obtained_marks)+1;
if($rep->id === $id){
$input['position'] = $position;
$report->update($input);
} else {
$report->update(['position' => $position]);
}
}
After this foreach
We need $report->update($input); this again
And i try that but not working also
And i try to debug this place too if it is working or not
if($rep->id === $id){
dd('works');
$input['position'] = $position;
$report->update($input);
}
With that dd('works');
And its not working too
 
that means there's something wrong
try dd( $all_reports->get() )
 
Yeah
 
before the foreach
 
$rep->id == $id
This part
 
9:04 AM
it works with two equals ?
 
Here we had three equals. I make it two
yes
 
now I am testing it further
 
Yeah changing the positions but in a wrong behavior
In short
Its still not updating all the positions
And we must need to update all the positions after changing just one obtained marks. Then we will get the right thing'
 
9:08 AM
ok
check if $all_reports->get()
contain all the reports you want to change the position on
 
ok sir
yeah
That's fine
 
now we'll try to replace $report->update($input);
by
$report->update($position);
no wait
replace
 
ok sir
 
$report->update(['position' => $position]);
$report->position = $position;
$report->save();
by
 
I don't have $report->save() there
oh fine
got it
I updated this
$report->update(['position' => $position]);
by this
$report->position = $position;
$report->save();
I guess you mean that
 
9:13 AM
yes
 
But the same behavior again
:(
updating one by one
 
what you mean
?
 
I mean it just update the position of that one record which I updated
And we need to update the position of the whole record
 
now to make sure
add dd
to the else clause
 
Because you know that after changing one position in the class disturb all the positions
 
9:15 AM
we're already looping through all of them
and update the position
 
Yeah
 
as you can see in the code
 
Yeah code looks fine
 
so now we need to see why the logic is not working
 
ok sir
 
9:16 AM
good
so now
add the dd to the else clause
 
ok sir
dd('works');
"works"
It's hitting that part also
Let me just show you I changed the variable in the foreach because I have the same vairable name up in the code too. So just review it
foreach( $all_reports->get() as $rep) {
$position = array_search( $rep['obtained_marks'], $obtained_marks)+1;
if($rep->id == $id){
$input['position'] = $position;
$report->update($input);
} else {
$report->position = $position;
$report->save();
}
}
$rep I updated
I showed it to you because sometimes I changed the variable names and it create problems for us
 
$report->update($input);
} else {
$report->position = $position;
$report->save();
change it in those lines too
 
ok sir
 
and try again
 
Everything is updating now except position
 
9:27 AM
not sure what you mean
 
I mean
 
by everything is updating
 
obtained marks is updating
 
we update just the position
 
but not the position
 
9:28 AM
do you mean in the selected record ?
or in all the records ?
 
In the selected record I update the obtained marks
I found that updated in the table
But not its position
In all the records
 
The position is not updating
 
check the dd( $obtained_marks )
 
ok sir
 
9:30 AM
after the foreach($all....) like
 
ok sir
array:2 [▼
0 => "61"
1 => "60"
]
This is the right thing
 
now
after the if(..) line
dd( $position );
 
ok sir
2
 
is it right ?
 
yeah
 
9:33 AM
for the selected record
 
Yeah
I checked
 
now
dd($input);
before : $report->update($input);
 
ok sir
But I noticed something
$rep->position = $position;
 
tell me
 
this line in the else statement
$position = array_search( $rep['obtained_marks'], $obtained_marks)+1;
and that is $position
if the id matches then we wanna store $position
else we will store the old position
 
9:35 AM
no
all the positions must change
if the id match
then we change position and the other data in the $input
 
Oh ok sir got it
 
ok
let us say what the dd print
 
remove the old dd
 
array:14 [▼
"_method" => "PATCH"
"_token" => "Xc5xHBonG0tSce1OQsqp6h51oSntS37Vx9g3VpZ8"
"class_id" => "10"
"student_id" => "abbas-khan-9603"
"student_name" => "abbas khan"
"subject" => "Maths"
"teacher_name" => "hasnain khan"
"report_categories_id" => "1"
"report_categories_name" => "Final"
"total_marks" => "100"
"obtained_marks" => "61"
"percentage" => 61.0
"grade" => "B"
"position" => 1
]
 
9:37 AM
as you can see
the position must be 2
not one
in the input
 
Yeah
 
that strange
show me the whole code again
 
ok sir
public function update(Request $request, $id)
{
$report = Report::findOrFail($id);
$input = $request->all();

$input['report_categories_name'] = ReportCategories::find( $input['report_categories_id'] )->name;

if (is_numeric($input['obtained_marks'])) {
$percentage = (($input['obtained_marks']/$input['total_marks']) * 100);
} else {
$percentage = 0;
}

$input['percentage'] = $percentage;

if ($percentage >= 0 && $percentage <= 49) {
$grade = 'D';
} elseif ($percentage >= 50 && $percentage <= 59) {
Small things in real life but big complex logic in code haha
 
try
dd($input['position']);
$input['position'] = $position;
dd($input['position']);
 
ok sir
2
 
9:42 AM
remove now the first dd
no
 
Yeah removed
 
now
dd($input, $input['position']);
 
ok sir
For the 76 id I have position 1 in the database
Which is wrong
But with the dd its showing position 2
Its write
 
yeh that why I want to see the $input content
to see the value of the position inside it
 
Now its strange again
Let me show you then explain
array:14 [▼
"_method" => "PATCH"
"_token" => "Xc5xHBonG0tSce1OQsqp6h51oSntS37Vx9g3VpZ8"
"class_id" => "10"
"student_id" => "abbas-khan-9603"
"student_name" => "abbas khan"
"subject" => "Maths"
"teacher_name" => "hasnain khan"
"report_categories_id" => "1"
"report_categories_name" => "Final"
"total_marks" => "100"
"obtained_marks" => "61"
"percentage" => 61.0
"grade" => "B"
"position" => 2
]

2
 
9:46 AM
ok
 
Now I have both first positions in the table
But here its outputing 2
I mean
In the output
We are getting what we want
But in the table its totally strange
 
yes
now try to remove the dd
and check the table
 
ok sir
After updating
for the highest marks I have position 2
Now let me make highest marks another one
 
I guess we've something wrong
in our logic
 
yes
Now what happened
 
9:49 AM
wait
 
I make another one the highest marks and goes to position 2
ok sir
 
we'll add the currently added mark to the $obtained_marks array
 
okay sir
 
add
array_push($obtained_marks, $report['obtained_marks']);
before
$json = json_encode($obtained_marks);
 
I noticed another thing too
ok sir
 
9:53 AM
wait
 
ok sir
 
must be
array_push($obtained_marks, $input['obtained_marks']);
instead
 
okay sir
 
now we may need to remove the old mark too
 
Now I have the position 3 too
Even i have two records
 
9:55 AM
$obtained_marks = $all_reports->where('id', '!=', $id)->pluck('obtained_marks')->toArray();
update this line
and check again
 
ok sir
nothing is updating now
if you are busy now then its okay. I will play with it and after sometime we will discuss it again
Because it took so much of your time
 
Yes
I'll back later
 
ok sir
 
 
1 hour later…
11:26 AM
Hi sir
I try my best but I just figure out one thing
That until the if statement everything is ok
So something is wrong with the if statement
That's all I can say. I have no idea after this that what is wrong
 
ok
tell me what happen right now
where we're
what works and what not
?
 
11:49 AM
The only issue that its positioning wrong
After updating the obtained marks of a record
Sometimes its updating the highest marks to 2 position
Sometimes both 2
Sometimes both 1
Totally strange behavior
I noticed one more thing
When I increase the marks two times
Then its position is changing
Correctly
I mean one was 67 marks and have first position
Another was 65 and second position
So I make the 67 to 70
the position was not changing
When I make it 75
Then it shifts to first position
And that 65 marks shift to second position
 
12:12 PM
Yeah that is the exact behvior now
I test it again
Every time I need to update two times a value to get the right position
And I have no idea why this is happening
 
12:23 PM
@GameO7er the question kinda old but if you were in a hurry and needed a nice admin panel i will suggest voyager voyager.devdojo.com
 
 
2 hours later…
2:17 PM
Hi sir
I figure it out completely I think
That is the only issue I explained above
We need to find a way for that now
Haha sir I just applied a silly appraoch
That looks awkward
But its working completely fine
That's what we expected
I just copied the same code we were working with and past it at the end. So it will get that process again
And it will work like that as I was updating it two times
Let me know if this is fine way to do it
 
 
2 hours later…
3:55 PM
Take a look please when you have time :)
 
 
2 hours later…
5:38 PM
Hi
 
6:10 PM
hi sir @ZakariaAcharki
 
6:24 PM
Hi
it's not ok to leave it like you did
since it will generate duplicate queries
 
haha yeah I was not sure about this. That's why I tell you
No its not generating
It works fine in every sense
 
I mean the queries execute two times
 
yeah
 
which is not good in the future
means it may slow down your system
 
yeah because if this I tell you
Yeah
in more data
 
6:25 PM
when you've more data ...
 
Yeah
 
ok
So now I quit
 
Ok sir
 
tomorrow we'll see why it's not working as expected from the first shot
 
Thank you for your time and support :)
Ok sir
 
6:26 PM
You're welcome bro
 
insha'Allah
Allah Bless you :)
Bye :)
 
see you tomorrow inshallah
 
See you :)
 
insha'Allah
 

« first day (131 days earlier)      last day (217 days later) »