mardi 25 février 2020

To create and update attendance system in laravel.. Having issue in array storing

I have undergoing a project of student management system where i have certain number of batches for month so each batch will have a max of 20 students, As well as each batch will have certain dates (Morning session and Afternoon session). For Example : Batch A will have 18 Students and will have 3 dates like DD-MM-YYYY, DD-MM-YYYY, DD-MM-YYYY. As a admin i need to register attendance for each student, each batch, each date and each session.

It means i have a consolidated screen for one batch including students, dates and sessions.

when i click batch it should show the batch students as well as batch dates, under batch dates there should be check box when i click the checkbox it should be marked as present, if not should be marked as absent.

It is in the table view where header consists of dates. While the body rows consists of student name and checkbox matching the dates column.

All the datas should be posted in one go and also need to retrieve and update the datas.

I have tried using array to store the datas but all the datas storing into the database are not stored according to the need.

How to achieve this?

Tried codes are below..

In Controller..

public function get_add($id)
{
    $module = $this->module;

    $singleData = $this->batch->find($id);
    return view('admin.'.$module.'.add_edit', compact('singleData', 'module'));
}

public function post_add(Request $request, $id)
{
    $module = $this->module;
    // $this->attendance->fill($request->all());

    $dd = $request->schedule_id;
    if($dd){
        foreach($request->schedule_id as $key => $v){
            $data = array(
                'batch_id' => $id,
                'schedule_id' => $request->schedule_id [$key],
                'user_id' => $request->user_id [$key],
                'am_attendance_status' => isset($request->am_attendance_status [$key]) ? 1 : 0,
                'pm_attendance_status' => isset($request->pm_attendance_status [$key]) ? 1 : 0,
                'created_at' => new DateTime,
                'updated_at' => new DateTime,
            );
            // dd($data);
            Attendance::insert($data);
        }
      return redirect('admin/'.$module.'/')->with('success', 'Data has been updated');
    }else{
      return redirect('admin/'.$module.'/')->with('error', 'Data has not been updated');
    }

}

In Modal..

<?php namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\SoftDeletes;

class Attendance extends Authenticatable
{
   use SoftDeletes;
   protected $dates = ['deleted_at'];

   protected $table = 'attendance';
   protected $fillable = ['batch_id', 'schedule_id', 'user_id', 'am_attendance_status', 'pm_attendance_status'];

   public function user()
   {
     return $this->belongsTo('App\User', 'user_id');
   }

  public function batch()
   {
    return $this->belongsTo('App\Batch', 'batch_id');
   }

   public function schedule()
   {
     return $this->belongsTo('App\Schedule', 'schedule_id');
   }

 }

In Route..

 //Attendance
Route::get('attendance', 'Admin\AttendanceController@get_index');
Route::get('attendance/{id}/add', 'Admin\AttendanceController@get_add');
Route::post('attendance/{id}/add', 'Admin\AttendanceController@post_add');

In View..

<div class="table-responsive text-center">
<table id="dataTable" class="table table-bordered table-hover" style="white-space: nowrap;">
    <thead>
        <th>#</th>
        <th>NRIC</th>
        <th>Student Name</th>
        @foreach($singleData->schedule as $list)
        <th class="text-center" colspan="2"></th>@endforeach
    </thead>
    <thead>
        <th></th>
        <th></th>
        <th></th>
        @foreach($singleData->schedule as $list)
        <th class="text-center"></th>
        <th class="text-center"></th>
        @endforeach
    </thead>
    @php $students = App\StudentHasCourse::with('user')->where('batch_id', $singleData->id)->get(); @endphp
    <?php $count = 0; ?>
        @foreach($students as $row)
        <?php $count++; ?>
            <tr>
                <th style="font-weight: normal;"></th>
                <th style="font-weight: normal;">@foreach($row->user->student as $stud)  @endforeach</th>
                <th style="font-weight: normal;"></th>
                @foreach($singleData->schedule as $list)
                <input type="hidden" name="batch_id[]" value="">
                <input type="hidden" name="user_id[]" value="">
                <input type="hidden" name="schedule_id[]" value="">
                <td>
                    <input type="checkbox" name="am_attendance_status[]" value="1">
                </td>
                <td>
                    <input type="checkbox" name="pm_attendance_status[]" value="1">
                </td>
                @endforeach
            </tr>
            @endforeach
</table>

Database Fields..

id 
user_id 
batch_id 
schedule_id 
am_attendance_status 
pm_attendance_status 
created_at 
updated_at 
deleted_at

View image link.. https://ibb.co/r485jYX




Aucun commentaire:

Enregistrer un commentaire