How can I save records in attendance table using Laravel?

Viewed 32

I am having issue with saving attendance record of student in attendance listings. The frontend part is working well but record saved in backend of attendance table is not shown. How can I save record in backend table of attendance which consists of level_id, teacher_id and student_id

Here is my attendance migrations table

$table->id();
        $table->unsignedBigInteger('level_id');
        $table->unsignedBigInteger('teacher_id');
        $table->unsignedBigInteger('student_id');
        $table->foreign('level_id')->references('id')->on('levels');
        $table->foreign('teacher_id')->references('id')->on('teachers');
        $table->foreign('student_id')->references('id')->on('students');            
        $table->date('attendance_date');
        $table->string('attendance_status');
        $table->timestamps();

Here is my students migrations tables

$table->id();

        // The Parents table must exist and Must have 'id'  as Primary Key
        $table->unsignedbiginteger('parent_id');
        $table->unsignedbiginteger('level_id');

        $table->foreign('parent_id')->references('id')->on('parents')
              ->onDelete('cascade');
        $table->foreign('level_id')->references('id')->on('levels');
        $table->string('student_roll_no');                  
        $table->string('student_surname');
        $table->string('student_middle_name')->nullable();
        $table->string('student_given_name');
        $table->string('student_place_of_birth');
        $table->date('student_date_of_birth');
        $table->string('student_gender');
        $table->text('student_home_address');
        $table->string('student_suburb')->nullable();            
        $table->string('student_post_code');
        $table->string('student_home_phone')->nullable();
        $table->string('student_work_phone')->nullable();
        $table->string('student_mobile_phone');
        $table->string('student_email')->nullable();
        $table->string('student_photo')->nullable();
        $table->string('language_spoken_at_home')->nullable();
        $table->string('school_name');
        $table->string('student_semester')->nullable();
        $table->string('school_suburb')->nullable();
        $table->text('school_address')->nullable();            
        $table->string('student_oversea_full_paying')->nullable();
        $table->string('emergency_person_one_name')->nullable();
        $table->string('emergency_person_one_mobile_number');
        $table->string('emergency_person_one_house_number')->nullable();
        $table->string('emergency_person_two_name')->nullable();
        $table->string('emergency_person_two_mobile_number')->nullable();
        $table->string('emergency_person_two_house_number')->nullable();
        $table->string('medical_condition')->nullable();
        $table->boolean('medical_health_support')->nullable();
        $table->boolean('family_court_orders');
        $table->string('family_court_file')->nullable();

        $table->boolean('authority_to_school_staff');
        $table->boolean('authorize_school_staff_to_arrange_medical_treatment');
        $table->boolean('authorize_school_staff_administering_medication');
        $table->boolean('notify_the_school_absent');
        $table->boolean('withdraw_child_from_school');
        $table->boolean('authorize_photograph_to_school');
        $table->boolean('authorize_child_name_school_newsletter_website');
        $table->boolean('authorize_short_local_walks');
        $table->boolean('authorize_participate_in_any_incursions');
        $table->boolean('information_contained_in_this_form_correct');

        $table->boolean('status')->default(1);

        $table->timestamps();

Here is my levels tables

$table->bigIncrements('id');            
        $table->string('level_name');
        $table->timestamps();

Here is my Teachers migrations tables $table->id();

        // The Parents table must exist and Must have 'id'  as Primary Key
        $table->unsignedbiginteger('user_id')->nullable();
        $table->foreign('user_id')->references('id')->on('users');

        $table->string('teacher_name'); 
        $table->string('teacher_email')->unique();           
        $table->string('teacher_home_phone')->nullable();
        $table->string('teacher_mobile_phone');
        $table->string('teacher_work_phone')->nullable();
        $table->string('teacher_home_address');
        $table->string('teacher_suburb')->nullable();
        $table->string('teacher_postcode');
        $table->timestamps();

Here is my Attendance Controller

    <?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;

use App\Models\Levels;
use App\Models\Teacher;
use App\Models\Student;
use App\Models\Attendance;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AttendanceController extends Controller
{
    /**
     * Create a new controller instance
     * 
     * @return void
     */
    public function __construct()
    {        
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard
     * 
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index($level_id = NULL)
    {
        
        $levels = Levels::all();
        $students = Student::all();                
        return view('admin.attendance.list', compact( 'levels', 'students','level_id'));
        
    }

    /**
     * Perform Actions in attendance.add
     * 
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function add()
    {
        $levels = array();
        $students = array();
        return view('admin.attendance.add', compact('levels', 'teachers'));
    }

    /**
     * Store values in application dashboard
     * 
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function store(Request $request, $level_id)
    {
        //get form data
        $data = $request->all();
        

        //Creeate Student record
        $students = Student::all();
        $levels = Levels::all();
        if($level_id){

            $levels = Levels::find($level_id);
            if($levels){
                $attendance = Attendance::with(['student', 'levels'])->first();
                return view('admin.attendance.add', compact('students','level_id', 'levels', 'attendance'));            
            }            
        }

    }
}

Here is my Attendance model

public function student()
{
    return $this->belongsTo(Student::class,'student_id');
}
public function teacher()
{
    return $this->belongsTo(Teacher::class, 'teacher_id');
}
public function levels()
{
    return $this->belongsTo(Levels::class, 'level_id');
}

Here is my list.blade.php file of containing attendance

    @section('content')    
    @if(session()->has('message'))
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-success">
                    {{ session()->get('message') }}
                </div>
            </div>
        </div>
    @endif
    @if(isset($levels) || $levels == '')
    <div class="form-row justify-content-center">
        <div class="form-group col-xs-6">
        <label for="Date"> Please Select Date</label>        
        <input type="date" name="attendance_date" value="{{ date('Y-m-d') }}" class="form-control" required>
        </div>
        <div class="form-group col-xs-6">
            <label for="Attendance">Please Select Level to see registered students</label>
            <select class="form-control" id="level_id" name="student_id">
                <option value="" disabled selected>Select Level</option>
                @foreach($levels as $level)
                <option value="{{@$level->id}}">{{@$level->level_name}}</option>
                @endforeach
            </select>
       </div>  
       </div>  
       
        @endif
        
@stop
@section('js')
   <script>
      jQuery(document).ready(function($) {
        // get your select element and listen for a change event on it
        $('#level_id').change(function() {
          // set the window's location property to the value of the option the user has selected
          window.location = '/attendance/add/'+$(this).val();
        });
      });      
   </script>
   
@endsection

Here is my add.blade.php file containing attendance

    <form action="{{ route('attendance.index') }}" method="GET" class="w-full max-w-xl px-6 py-12" enctype="multipart/form-data"> 
  @csrf
  @php
        $heads = [
            'Name',
            'Roll Number',                                                
            'Semester',
            ['label' => 'Attendance', 'no-export' => true, 'width' => 5],
        ];

        
        /*$btnDetails = '<button class="btn btn-xs btn-default text-teal mx-1 shadow" title="Details">
                           <i class="fa fa-lg fa-fw fa-eye"></i>
                       </button>';*/

        $config = [
            'data' => $students,
            'order' => [[1, 'asc']],
            'columns' => [null, null, null, null, ['orderable' => true]],
        ];
        @endphp

        {{-- Minimal example / fill data using the component slot --}}
        <x-adminlte-datatable id="table6" :heads="$heads" head-theme="light" theme="light custom-head-theme dt-responsive"
            striped>
            @if($config['data'])
                @foreach($config['data'] as $row)
                    <tr class="{{ (isset($row['status']) && $row['status']==0) ? 'table-danger' : ''}}">
                        <td>{!! $row['student_given_name']  !!}</td>                        
                        <td>{!! $row['student_roll_no']!!}</td>                                                                       
                        <td>{!! $row['student_semester']!!}</td>
                        <td>
                            <nobr>
                                <select class="form-control" name="attendance_status" value="{{old('attendance_status'), @$attendance->attendance_status}}" id="attendance_status" required>
                                    <option value="" {{@$attendance->attendance_status == ''  ? 'selected' : ''}} disabled selected>Select Option</option>
                                    <option value="Present" {{@$attendance->attendance_status == 'present'  ? 'selected' : ''}} selected>Present</option>
                                    <option value="Absent" {{@$attendance->attendance_status == 'absent'  ? 'selected' : ''}}>Absent</option>
                                </select>
                                <input type="text" name="textinput" id="level_id" placeholder="Reason">
                            </nobr>
                        </td>
                    </tr>
                @endforeach
            @endif
        </x-adminlte-datatable>
  
  
  <div class="row mt-3">
    <div class="col-md-12">
      <div class="card-footer">
        <div class="float-left col-md-4 mb-2">
          <button type="submit" name="save_close" value="true" class="btn btn-primary btn-lg btn-block">Save & Close</button> 
        </div>        
        <div class="float-right col-md-4 mb-2">
          <a href="{{ route('attendance.index') }}"><button type="button" class="btn btn-secondary btn-lg btn-block">Cancel</button></a>
        </div>                  
      </div>
    </div>
  </div>
</form> 
@stop

What modifications are required in attendance controller in order to save record in table and I can view it on frontend side as well

1 Answers

On your AttendanceController you just show data, not insert data to database, you should get the request data and insert data to database, but first check your blade file, you must make an input for level_id, teacher_id, and student_id to check your attachment you can use

dd($request); 
die();

on your first line AttendanceController function store

public function store(Request $request, $level_id)
    {

dd($request); 
die();

    //get form data
    $data = $request->all();
    

    //Create Student record
    $students = Student::all();
    $levels = Levels::all();
    if($level_id){

        $levels = Levels::find($level_id);
        if($levels){
            $attendance = Attendance::with(['student', 'levels'])->first();
            return view('admin.attendance.add', compact('students','level_id', 'levels', 'attendance'));            
        }            
    }

}

if your system catch the good request you should try this

 public function store(Request $request, $level_id)
    {
         $levels_id = $request->level_id;
         $teachers_id = $request->teacher_id;
         $students_id = $request->student_id;

         $data = [$levels_id,teachers_id,students_id];

         attendance::create($data);
        

    }

there's the code to save data into your laravel project, you should approve my solution

Related