Can you give some more information? What does "save the data i times" mean ? Do you mean "why does it save multiple times, when I clearly ask it to save multiple times?"
Why this code save the data i times
@Sinnbeck I want to save the attendance for the i number of employee at a time , but the code save the data i numbers at a time , for example if the number of employees are 4 then in the database there are 4 similar records at a time when i click save and so on , is that clear ?
@habte Not quite. You want it to add 4 records to the database, but it adds 4 records ? Can you explain exaclty what you expect to happen in the database and what actually happens in the database
@Sinnbeck no , lets say there are 4 employees . And i want to take one attendance for the specific time for those employees and i select the status for each employees and when i click the save button , it saves 4 similar attendances records that have 4 employees each records but, i want to save only 1 attendance record for 4 employees ...
@habte Ok so add 1 record to the database with all data? So $attend->employee_id is not an integer column, it is a json column ?
@Sinnbeck yes , attendance is one tabel in the database , and it contains employees status ,date and name of the employees .
@habte That is not what I asked? Maybe show the EmployeeAttendance migration if you dont understand what I am asking
@Sinnbeck ok
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStudentAttendancesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('student_attendances', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('student_class_id')->nullable();
$table->foreign('student_class_id')->references('id')->on('student_classes')->onDelete('set null');
$table->unsignedBigInteger('members_id')->nullable();
$table->foreign('members_id')->references('id')->on('members')->onDelete('set null');
$table->date('date');
$table->string('attend_status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('student_attendances');
}
}
member_id means employees id
@habte with this schema, you can only save 1 member per row
@habte So the migration does not match the code? There is no employee_id on that table? Or are you renaming things while asking questions?
@Sinnbeck yeah i changed the code see it
@habte Where?
@Sinnbeck ops
<?php
namespace App\Http\Controllers;
use App\Models\Student_attendance;
use Illuminate\Http\Request;
use App\Models\Student_class;
use Illuminate\Support\Facades\Auth;
class StudentAttendanceController extends Controller
{
public function view(){
$attendances = Student_attendance::all();
return view('backend.class_attendance.view',['attendances'=>$attendances]);
}
public function select_class(){
$class = Student_class::all();
return view('backend.class_attendance.select_class',['class'=>$class]);
}
public function add($id){
$class = Student_class::find($id);
$students = $class->students;
return view('backend.class_attendance.add',['students'=>$students, 'class'=>$class]);
}
public function store(Request $request)
{
$countemployee = count($request->employee_id);
for ($i = 0; $i < $countemployee; $i++) {
$attend_status = 'attend_status' . $i;
$attend = new Student_attendance();
$attend->date = date('Y-m-d', strtotime($request->date));
$attend->student_class_id = $request->class_id;
$attend->members_id = $request->employee_id[$i];
$attend->attend_status = $request->$attend_status;
$attend->save();
}
$notification = array(
'message' => 'Class Attendance Submitted',
'alert-type' => 'success'
);
return redirect()->route('StudentAC.view')->with($notification);
}
public function details($date){
$data['details'] = Student_attendance::where('date',$date)->get();
return view('backend.class_attendance.detail',$data);
}
}
@habte Then the code you have written should work. You need 1 row in the database for each student, as the members_id column only takes 1 ID, not multiple
@Sinnbeck it works , but what i want to ask was it saves #i times attendance instead of 1 attendance .
@habte So there is only 1 member id ?
$attend->members_id = $request->employee_id[$i]; //always the same?
@Sinnbeck no there may be many members but what i want to do is saving one attendance at a time for a given number of members , but in the above code the number of attendances is equal to the number of members.but i want to save one attendance for any number of members for each time when i click the save buttons
@habte Then you add a row for each. How do you expect that single row to look in the database if you were to save them all to one row ?
@habte your table had exactly 1 id per row, so there is no way to store multiple member in 1 row
@Sinnbeck so in the above code if i have 100 members and when i want to take the attendance for Monday , there are 100 attendances that have each attendance contains 100 memebers status ? But what i want to do is one attendance for monday that have 100 members status. Do you get my idea ?
@habte that how a relational database work
100 attendance for 100 user is how it supposed to work, if you just want 1 row to contain all user in 1 day, then you shouldnt use a relational database
@Lumethy i don't understand what you want to say ,can you show me the database table for what you sayed
@habte So what would you expect to put inside the members_id column? 0 ? It only takes 1 number
@Sinnbeck what is 1 number ? I think we are not on the same page so I will show you the screen shoot for what i was talking . But if you can ,can you show me the migrations for the attendance tale that save one attendance for many memebers ?
@habte it is not possible to save 1 attendance for many members
@habte this is getting more complicate :). You will get 10 rows of attendance id 1 with 10 different member id. You can't get 1 row of attendance with 10 different member id.
@Sinnbeck it is not memebers id , i am talking about the attendance table , it works ,but lets say i want to take attendance for Monday and when i click the attendance button it comes with the members that are in the class , and i choose the status for each members and finally i click the save button , and when i want so see the attendance i except to have 1 attendance for monday and when i click it it contains all the status of the members in that day , but what i see is there are numbers of attendance that are equal to number of members but each attendance contain similar data and number of memebers .... if it is not clear i will show you the screen shoots for what i was saying .
@Lumethys how can i get 1 row attendance with different members
@habte Looks at your migration again. Dont you see the column ?
Schema::create('student_attendances', function (Blueprint $table) {
$table->id();
$table->foreign('members_id')->references('id')->on('members')->onDelete('set null'); //<- this is what I am talking about
});
@Sinnbeck i will show you the screen shoots after some hours
@Sinnbeck sorry for the late , here is the image for the attendances "https://i.imgur.com/E7cnGMH.png" and when I click details for the date "13-10-2022" it contains similar data like this one "https://i.imgur.com/n7ImReh.png" since I have only two members in the class there are two attendances in date "13-10-2022" and each of them contain 2 members ! but what I want to do is I want to take 1 attendance for date "13-10-2022" that contains two members ! do you get my idea ?
@habte what you want is simply not possible
I say the original code you had was working fine, and it's also the proper way to store things in a relational database.
Storing json or an array in the database is a bad practice, that unfortunately is gaining popularity.
I suggest giving this a read and you might hopefully do it the proper way.
It is definitely about database design. If you must have only one row in the student_attendance table, then try moving the employee_id/members_id (whatever the heck you call it?) Into a pivot table.
Please or to participate in this conversation.