Want to ask why my data from the database (MySQL) doesn't all appear on the website from the grades table join with the grade_details table, my database is attached:
Screenshot my Database in MySQL
So this data is from the join grades table with join grade_details taken from the id_grade data like the table above.
Screenshot in web
As given by the blue contact in the Quiz table, only Student31 values appear, while the other Quiz values are at 0, but the student names Servira and Student4 already have values, namely 90 and 80, as in the grade_details table in the database. But to avoid confusion, the Servira and Student4 data don't show up with a value of only 0. Even though I have used a join with another table, the other quiz scores cannot be read, only 1 student readable value data.
Code:
Model Grade Detail:
class GradeDetail extends Model
{
use HasFactory;
protected $primaryKey = 'id_grade_detail';
protected $table = 'grade_details';
protected $fillable = [
'id_grade_detail',
'id_grade',
'id_student',
'quiz',
'assignment',
'd_t',
'min_text',
'final_text',
'total',
];
public function grades(){
return $this->belongsTo(Grade::class, 'id_grade','id_grade');
}
public function students(){
return $this->belongsTo(User::class, 'id_student','id');
}
}
Mode Grade:
class Grade extends Model
{
use HasFactory;
protected $primaryKey = 'id_grade';
protected $table = 'grades';
protected $fillable = [
'id_grade',
'id_subject',
'id_academic_year',
'id_semester',
'min_score',
'done',
];
public function details(){
return $this->belongsTo(GradeDetail::class, 'id_grade','id_grade');
}
}
Controller:
public function ViewGrade($id){
$subject = Grade::join('subjects', 'grades.id_subject', '=', 'subjects.id_sub')
->join('class_infos', 'subjects.id_class', '=', 'class_infos.id')
->join('class_details', 'class_infos.id', '=', 'class_details.id_class')
->join('users', 'class_details.id_user', '=', 'users.id')
->where('subjects.id_teacher', '=', Auth::user()->id)
->where('grades.id_grade', '=', $id)
->get();
return view('teacher.grade.view_grade', compact('subject'));
}
Blade:
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead class="text-xs text-white uppercase bg-[#464867] dark:bg-[#464867]">
<tr>
<th scope="col" class="py-3 px-6">
No
</th>
<th scope="col" class="py-3 px-6">
NISN
</th>
<th scope="col" class="py-3 px-6">
Name Student
</th>
<th scope="col" class="py-3 px-6">
Min Score
</th>
<th scope="col" class="py-3 px-6">
Quiz
</th>
<th scope="col" class="py-3 px-6">
Assignment
</th>
<th scope="col" class="py-3 px-6">
Daily Tests
</th>
<th scope="col" class="py-3 px-6">
Min Exam
</th>
<th scope="col" class="py-3 px-6">
Final Exam
</th>
<th scope="col" class="py-3 px-6">
Total
</th>
</tr>
</thead>
<tbody>
#php $no = 1; #endphp
#forelse($subject as $data)
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$no++}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->username}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->name}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-red-700 whitespace-nowrap dark:text-white">
{{$data->min_score}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{ (!empty( $data->id == $data->details->id_student )) ? $data->details->quiz: 0}}
#else
0
#endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{$data->details->assignment}}
#else
0
#endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{$data->details->d_t}}
#else
0
#endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{$data->details->min_text}}
#else
0
#endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{$data->details->final_text}}
#else
0
#endif
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
#if(!empty($data->details))
{{$data->details->total}}
#else
0
#endif
</th>
</tr>
#empty
<tr colspan = "10" class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
No Data
</td>
</tr>
#endforelse
</tbody>
</table>
How do you make the Servira and Student4 column name fields filled with grades on quizzes on the web as well as in the grade_details database table?
in your model Grade you should specify the details relationship as a HasMany relation not a BelongsTo relation so your model would be :
public function details(){
$this->hasMany(GradeDetail::class, 'id_grade','id_grade');
}
#AlirezaSalehi is correct. It is indeed a hasMany relationship. The correct details relationship in your Grade is indeed:
public function details(){
$this->hasMany(GradeDetail::class, 'id_grade','id_grade');
}
In the GradeDetail model, your primary key is id_grade_detail (not id), which is fine, so your relationships should be as following:
public function grades(){
return $this->belongsTo(Grade::class, 'id_grade','id_grade_detail');
}
public function students(){
return $this->belongsTo(User::class, 'id_student','id_grade_detail');
}
Where id_student is the primary key of the User model, which I assume by the code you have given (out of the box, the primary key of the User model is id, did you change that?)
Check the documentation at https://laravel.com/docs/9.x/eloquent-relationships#one-to-many
In my experience, it's good practice to follow the Laravel conventions, explained on the same page. Unless you have no other choice, f.ex. if the DB is already in existence. If it's a fresh project, consider to follow the conventions. It will safe you debugging time later on. (speaking of personal experience)
I want to edit data using an array, but when I submit the edits, the results are all the same from below.
When editing each student name, it has a different value, but when submitting, the results are all the same, like this in MySQL and the Web:
Code:
Controller
public function UpdateEditGradeStudent(Request $request){
$data = GradeDetail::find($request->id_grade_detail);
foreach($request->id_grade as $index => $value) {
DB::table('grade_details')
->update(array(
'id_grade' => $value,
'quiz' => $request->quiz[$index],
'assignment' => $request->assignment[$index],
'min_text' => $request->min_text[$index],
'd_t' => $request->d_t[$index],
'final_text' => $request->final_text[$index],
'total' => $request->total[$index],
));
}
return redirect('/');
}
Blade
<form method="POST" action="{{route('edit.teacher.grade')}}" enctype="multipart/form-data">
#csrf
{{-- <input type="hidden" name="id_grade[]" value="{{ $userGrades->id_grade }}">--}}
<div class="flex">
<input type="submit" class="focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 mt-3 mr-4" type="button" value="Save">
<a href="{{url('teacher/data-grade')}}" class="focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 mt-3" type="button" >Cancel</a>
</div>
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<tbody>
#php $no = 1; #endphp
#forelse($subject as $data)
<input
type="hidden"
name="id_grade[]"
value="{{$data->id_grade}}"
>
<tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$no++}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->username}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
{{$data->name}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-red-700 whitespace-nowrap dark:text-white">
{{$data->min_score}}
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
<input
type="number"
min="0"
max="100"
name="quiz[]"
id="quiz"
value="{{$data->quiz}}"
autocomplete="quiz"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
required
>
</th>
<th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
<input
type="number"
min="0"
max="100"
name="assignment[]"
id="assignment"
value="{{$data->assignment}}"
autocomplete="min_score"
class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
required
>
</th>
I didn't screenshot all the blade coding.
How to edit data array according to the data we have edited data.
Looks like your key is id_grade_detail, you should filter by that field:
#forelse($subject as $data)
<input
type="hidden"
name="id_grade_detail[]"
value="{{$data->id_grade_detail}}"
>
<input
type="hidden"
name="id_grade[]"
value="{{$data->id_grade}}"
>
...
And on the server:
public function UpdateEditGradeStudent(Request $request)
{
foreach($request->id_grade_detail as $index => $value) {
$grade_detail = GradeDetail::find($value);
if ($grade_detail) {
$grade_detail->quiz = $request->quiz[$index];
$grade_detail->assignment = $request->assignment[$index];
$grade_detail->min_text = $request->min_text[$index];
$grade_detail->d_t = $request->quiz[$index];
$grade_detail->final_text = $request->final_text[$index];
$grade_detail->total = $request->total[$index];
$grade_detail->save();
}
}
return redirect('/');
}
I have 2 columns of information,check in and check out columns. I filled in these 2 columns by retrieving data from attendance_records_table. Below are the code to retrieve data from the table,
<tbody class="bg-white divide-y divide-gray-200">
#php
$no = 1
#endphp
#foreach ($user as $row)
<tr>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900"> {{ $no++ }} </div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900"> {{ $row->staff_id }} </div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900"> {{ $row->name }} </div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900"> {{ $row->email }} </div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ $row->Department->department ?? null }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ $row->company_name }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> View profile </td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"></td>
#php
$latestRecord = App\Models\AttendanceRecord::orderBy('created_at',
'desc')->first();
#endphp
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$latestRecord -> date_checkIn }} <br>
{{$latestRecord -> time_checkIn }} <br>
{{$latestRecord -> location_checkIn }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> <br>
{{$latestRecord -> time_checkOut }} <br>
{{$latestRecord -> location_checkOut }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> Edit </td>
</tr>
#endforeach
</tbody>
The problem now is that it is updating all the check in and check out column with the same information no matter who the person is. How can I make it specific to staff_id number? Here I attach 2 screenshot so that you can understand the situation better. https://paste.pics/f4f994f575051e31d3b5d39371a4a54b, https://paste.pics/8ab5293c4830c84e271d48c10c152149
I updated the information through postman, here are the 2 controllers for clock in and clock out :
public function userClockIn(Request $r)
{
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkIn', 'location_checkIn'])->first();
$mytime = Carbon::now('Asia/Kuala_Lumpur');
$date = $mytime->format('Y-m-d');
$time = $mytime->format('H:i:s');
$users->date_checkIn = $date;
$users->time_checkIn = $time;
$users->location_checkIn = $r->location_checkIn;
$users->save();
// Retrieve the current data
$currentData = $users->toArray();
// Store current data into attendance record table
$attendanceRecord = new AttendanceRecord();
$attendanceRecord->fill($currentData);
$attendanceRecord->save();
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
public function userClockOut(Request $r)
{
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'time_checkOut', 'location_checkOut'])->first();
$mytime = Carbon::now('Asia/Kuala_Lumpur');
$date = $mytime->format('Y-m-d');
$time = $mytime->format('H:i:s');
$users->date_checkIn = $date;
$users->time_checkOut = $time;
$users->location_checkOut = $r->location_checkOut;
// Save the updated data to the database
AttendanceRecord::updateOrCreate(
['staff_id' => $users->staff_id, 'date_checkIn' => $date],
$users->toArray()
);
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
I have a livewire component that performs queries on a table in sql but I want to select the query that I want to save in another table in mysql
my component and my view
{
return view('livewire.search-users', [
'users' => viewfacturaa::where('FACTURA', $this->search)
->get(),
]);
}
<div>
<input wire:model="search" type="text" placeholder="Ingrese factura" />
<table class="min-w-full">
<thead class="bg-info dark:bg-white">
<tr>
<th scope="col"
class="py-3 px-6 text-xs font-medium tracking-wider text-left text-white uppercase dark:text-gray-400">
SELECCION
</th>
<th scope="col"
class="py-3 px-6 text-xs font-medium tracking-wider text-left text-white uppercase dark:text-gray-400">
FACTURA
</th>
<th scope="col"
class="py-3 px-6 text-xs font-medium tracking-wider text-left text-white uppercase dark:text-gray-400">
PRODUCTO
</th>
<th scope="col"
class="py-3 px-6 text-xs font-medium tracking-wider text-left text-white uppercase dark:text-gray-400">
DESCRIPCION
</th>
</tr </thead>
<tbody>
#foreach ($users as $user)
<tr>
<td class="border px-2 py-2 text-xs">{{$user->FACTURA}}</td>
<td class="border px-2 py-2 text-xs">{{$user->PRODUCTO}}</td>
<td class="border px-2 py-2 text-xs">{{$user->DESCRIPCION_FACTURA}}</td>
<td class="border px-2 py-2 text-xs text-center">
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
As can be seen in my view, it is a text type input in which I write the invoice number and the results are displayed in a table
I have a table that presents grouped data, and has 'buttons' in the form of hyperlinks for users to view more information about that grouped data as a whole. However the button also loops with every row, when ideally I would only like it to show once for every group. I have tried moving around my #foreach loops but keep getting errors that the requested ID does not exist in this collection instance.
For example, I am trying to get those links to only appear once in each group (once in 1 and 2 like in the image).
Code:
<tr>
#foreach ($grouped as $mealplan)
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Information
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Meal Plan Info
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Print
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Archive
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Name
</th>
<th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Day
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
#foreach ($mealplan as $recipe)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
More
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
Meal Plan Info
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
Print Ingredient List
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
Archive
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{$recipe->recipe_name}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{$recipe->Day}}
</td>
</tr>
</br>
#endforeach
#endforeach
Any advice on where to move the loops, or how to only get the links to show once per group in the table would be really appreciated.
The concept of doing such iteration is as follows:
Notice the comments in the code
<table>
{{--/*Comment: iterate through the groups to show sub-groups*/--}}
#foreach ($group as $sub_group)
<thead>
{{--/*Comment: the group head row is here*/--}}
</thead>
<tbody>
{{--/*Comment: iterate through the sub-group to show its items*/--}}
#foreach ($sub_group as $item)
{{--/*Comment: put here a condition to filter items*/--}}
{{--/*Comment: in your case you want the "MORE" link to only
appear in the first row so we will check iteration index.
Luckily, we have $loop variable in Laravel so we don't have to count
the index manually. The condition will be like this:
1- if loop is in round "1", we should print the link inside the <td>.
2- if not we should print empty <td> */--}}
{{--/*Comment: Print the "more" link only on the first iteration */--}}
#if($loop->iteration == 1)
<td>{{--/*Comment: put the link here */--}}</td>
#else
<td>{{--/*Comment: empty cell*/--}}</td>
#endif
{{--/*Comment: then here comes the rest of the <td>s */--}}
#endforeach
</tbody>
#endforeach
</table>
And here is an Edit to your code
<table class="table" style="magin-left:200px">
#foreach ($grouped as $mealplan)
<thead>
<tr>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Information
</th>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Meal Plan Info
</th>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Print
</th>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Archive
</th>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Recipe Name
</th>
<th scope="col"
class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Day
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
#foreach ($mealplan as $recipe)
<tr>
{{-- Print the "more" link only on the first iteration --}}
#if ($loop->iteration == 1)
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="recipeinformation/{{ $recipe->Recipe_ID }}"
class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">More</a>
</td>
#else
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium"></td>
#endif
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="/MealPlanDisplay/modal/{{ $recipe->MealPlan_ID }}"
class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Meal Plan Info</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="printpreviewingredients/{{ $recipe->MealPlan_ID }}"
class="text-indigo-600 hover:text-indigo-900 mb-2 mr-2">Print Ingredient
List</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
<a href="/Archive/{{ $recipe->MealPlan_ID }}"
class="text-red-600 hover:text-indigo-900 mb-2 mr-2">Archive</a>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{ $recipe->recipe_name }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
{{ $recipe->Day }}
</td>
</tr>
#endforeach
</tbody>
#endforeach
</table>
Outcome of the code