Home > Net >  How to redirect to new page using laravel
How to redirect to new page using laravel

Time:09-21

I am having issue redirecting to new page where I have added attendance module. The solution proposed was when teacher selects date and levels in attendance url it redirects to attendance/add/{id} url. The url display page is giving error of 404|Not found. How can this be resolved using laravel.

Here is my Attendance Controller

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();  
        if(isset($level_id) && $level_id){
            $students = Student::where('level_id', $level_id)->get();
        }      
        return view('admin.attendance.list', compact( 'levels', 'students','level_id'));
        
    }

    /**
     * Perform Actions in attendance.add
     * 
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function add($level_id = NULL)
    {
        $students = Student::all();
        $levels = levels::all();
        if($level_id){

            $levels = Levels::find($level_id);
            if($levels){
            return view('admin.attendances.add', compact('students','level_id', 'levels'));            
            }
        }
    }

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

        //Creeate Student record
        $students = Student::all();

        

        
        return redirect()-> route('attendance.add')->with('message', "Attendance information saved successfully!");
        
        
    }

    }

Here is my attendances/list.blade.php file

    @section('content_header')
    <div >
        <div >
            <div >
                <h1>Attendance List</h1>
            </div>
            @if(isset($level_id) && $level_id)
            <div >
                <a href="{{ route('attendance.index', $level_id) }}"><button type="button" >Add Attendance</button></a>
            </div>
            @endif
        </div>    
    </div>
@stop

@section('content')    
    @if(session()->has('message'))
        <div >
            <div >
                <div >
                    {{ session()->get('message') }}
                </div>
            </div>
        </div>
    @endif
    @if(!isset($level_id) || $level_id == '')
    <div >
        <div >
        <label for="Date"> Please Select Date</label>        
        <input type="date" name="attendance_date" value="{{ date('Y-m-d') }}"  required>
        </div>
        <div >
            <label for="Attendance">Please Select Level to see registered students</label>
            <select  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>  
       @else        
        {{-- Setup data for datatables --}}
        @php
        $heads = [
            'Name',
            'Roll Number',                                                
            'Semester',
            ['label' => 'Attendance', 'no-export' => true, 'width' => 5],
        ];

        
        /*$btnDetails = '<button  title="Details">
                           <i ></i>
                       </button>';*/

        $config = [
            'data' => $students,$levels,
            '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 >
                        <td>{!! $row['student_given_name']  !!}</td>                        
                        <td>{!! $row['student_roll_no']!!}</td>                                                                       
                        <td>{!! $row['student_semester']!!}</td>
                        <td>
                            <nobr>
                                <select  name="attendance_status" value="{{old('attendance_status'), @$attendance->attendance_status}}" i 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>
                                    <input type="text" name="textinput" id="absent" placeholder="Reason">
                                  </select>
                            </nobr>
                        </td>
                    </tr>
                @endforeach
            @endif
        </x-adminlte-datatable>
        @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 = '/attendances/add/' $(this).val();
        });
      });      
   </script>
@endsection

Here is my migration file of attendances

$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 are the web routes

Route::get('/attendance', 'App\Http\Controllers\Admin\AttendanceController@index')->name('attendances.index');
Route::get('/attendance/add/{id}', 'App\Http\Controllers\Admin\AttendanceController@store')->name('attendances.add');    

How can I resolve the issue in order to check form working properly. Need help.

CodePudding user response:

The issue is the wrong route name In the route file, it says attendance/add/{id}

in your script, you are using

  window.location = '/attendances/add/' $(this).val();

which is wrong (typo)

CodePudding user response:

Have you tried

<script>window.location = "{{ route('attendances/add') }}";</script>

or

<script>window.location.href = "{{url('attendances-full-path')}}";</script>

  • Related