Support the ongoing development of Laravel.io →
Laravel Input Views

I am trying to create a student's attendance. I have as students, klasses, attendances controllers; I also have an enrollments controller in order to manage the many to many relationship between students and klasses.

Everything is working more or less how it is supposed to. Moreover, on my create.blade.php page I have the attendance form which takes the amount of weeks a class has and creates a checkbox for each one of them in a for loop. The only part failing is when I post my form, it returns a 404 not found error even if I have my routes to look for that post and run the store function of my attendances controller.

This is my web.php:

//Post URL routes
Route::post('/attendances', [

  'uses' => 'Attendances@store',
  'as' => 'attendances.store'
]);

//"Students, Klasses, and attendances" should be renamed to StudentsController, KlassesController, and AttendancesController
Route::resource('students', 'Students');
Route::resource('klasses', 'Klasses');
Route::resource('enrollments', 'EnrollmentsController');
Route::resource('attendances', 'Attendances');

This is my attendances controller for my index, create and store functions:

<?php

namespace App\Http\Controllers;

use App\Attendance as Attendance;
use App\Student as Student;
use App\Klass as Klass;
use App\Enrollment as Enrollment;

use Illuminate\Http\Request;

class Attendances extends Controller
{
    public function index()
    {
        $attendances = Attendance::All();
        $enrollments = Enrollment::All();
        $klasses = Klass::All();

        return view('attendances.index', compact('klasses'));
    }

    public function create(request $request)
    {
        $k_id = (int)$request->id;
        $enrollments = Enrollment::where('klassID', $k_id)->get();
        $klass = Klass::find($k_id);

        return view('attendances.create', compact('enrollments', 'klass'));
    }

    public function store(Request $request)
    {
        dd($request);
    }

}

This is my create.blade.php for my attendances view:

@extends('layout')

@section('title')
    Attendance
@endsection

@section('content')

    <h1> Class : {{ $klass->name }}</h1>


    <form action="post" action=" {{ route('attendances.store') }} ">
        <table>
            <tr>
                <th> Students </th>

                @for($i = 1; $i <= $klass->durationWeeks; $i++)
                    <th> Week {{ $i }} </th>
                @endfor

            </tr>

            @foreach($enrollments as $enrollment)
                <tr>
                    <td>
                        {{ $enrollment->student->first_name }} {{ $enrollment->student->last_name}}
                    </td>

                    @for($i = 0; $i < $enrollment->klass->durationWeeks; $i++) 
                        <td>
                            <input type="checkbox" name="week" value="{{ $i }}">
                        </td>
                    @endfor
                </tr>
            @endforeach

        </table>
        <input type="submit">
    </form>

@endsection

By the time this form is submitted, my URL looks something like this:

http://localhost:8000/attendances/post?week=4&week=4

Please let me know if you need more information. Thanks in advance for all your help.

Last updated 3 years ago.
0

SOLUTION:

I found it out, everything is set up alright. However, on my create.blade.php for my attendances view. I specified post as an action, but it has to be specified as a method.

Which is why I got a 404 error; I had not set up anything for a /post URL.

I hope this post can help someone in the near future.

0

Thank you! I really appreciate the comment.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.