I am unable to store the value in the database using the register form

I have tried different ways like changing routes controllers here is my Student.php model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'email',
        'phone_number',
        'address',
        'select_course',
        'highest_qualification',
        'cv_file'
    ];

    public function courses()
    {
        return $this->belongsToMany(Course::class);
    }
}

here are my routes web.php

<?php
use App\Http\Controllers\StudentController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CustomAuthController;

    Route::get('/register', function () {
    return view('/register');
});

Route::post('/students',[StudentController::class,'store'])->name('students.store');
Route::get('/students', [StudentController::class,'index'])->name('students.index');
Route::get('/students/create', [StudentController::class,'create'])->name('students.create');
Route::get('/students/{id}', [StudentController::class,'show'])->name('students.show');
Route::get('/students/{id}/edit', [StudentController::class,'edit'])->name('students.edit');
Route::put('/students/{id}', [StudentController::class,'update'])->name('students.update');
Route::delete('/students/{id}', [StudentController::class,'destroy'])->name('students.destroy');

here is my StudentController

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use App\Models\Course;

class StudentController extends Controller
{
    public function index()
    {
        $students = Student::all();
        return view('students.index', compact('students'));
    }

    public function create()
    {
        $courses = Course::all();
        return view('students.create', compact('courses'));
    }

    public function store(Request $request)
    {
       
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:students,email',
            'phone_number' => 'required',
            'address' => 'required',
            'select_course' => 'required',
            'highest_qualification' => 'required',
            'cv_file' => 'required|file|max:10240',
        ]);

        $cv_file = $request->file('cv_file');
        $cv_file_path = $cv_file->store('cv_files');

        $student = new Student([
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'phone_number' => $request->get('phone_number'),
            'address' => $request->get('address'),
            'select_course' => $request->get('select_course'),
            'highest_qualification' => $request->get('highest_qualification'),
            'cv_file' => $cv_file_path,
        ]);
        $student->save();
       
        dd($request);

        $student->courses()->attach($request->get('courses'));

        return redirect('/students')->with('success', 'Student has been added');
    }

    public function show($id)
    {
        $student = Student::find($id);
        return view('students.show', compact('student'));
    }

    public function edit($id)
    {
        $student = Student::find($id);
        $courses = Course::all();
        return view('students.edit', compact('student', 'courses'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:students,email,'.$id,
            'phone_number' => 'required',
            'address' => 'required',
            'select_course' => 'required',
            'highest_qualification' => 'required',
            'cv_file' => 'nullable|file|max:10240',
        ]);

        $student = Student::find($id);

        if ($request->hasFile('cv_file')) {
            $cv_file = $request->file('cv_file');
            $cv_file_path = $cv_file->store('cv_files');
            $student->cv_file = $cv_file_path;
        }

        $student->name = $request->get('name');
        $student->email = $request->get('email');
        $student->phone_number = $request->get('phone_number');
        $student->address = $request->get('address');
        $student->select_course = $request->get('select_course');
        $student->highest_qualification = $request->get('highest_qualification');
        $student->save();

        $student->courses()->sync($request->get('courses'));

        return redirect('/students')->with('success', 'Student has been updated');
    }

    public function destroy($id)
    {
        $student = Student::find($id);
        $student->delete();
 
        return redirect('/students')->with('success', 'Student has been deleted successfully');
    }
}

and I am using register.blade.php file in which I made a form to register a new candidate this route I am using in the register form. when I click on submit button it getting refreshed

`<form
      method="POST" action="{{ route('students.store') }}">
      @csrf
`

and here is the dashboard where I click on for register a new candidate

`<a class="inline-block text-gray-400 no-underline hover:text-gray-200 hover:text-underline py-2 px-4" href="{{ url('/register') }}">Register a new candidate</a>`

here is my students_table migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone_number')->nullable();
            $table->string('address')->nullable();
            $table->string('select_course');
            $table->string('highest_qualification')->nullable();
            $table->string('cv_file')->nullable();
            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('students');
    }
};
2

There are 2 answers

3
zohrehda On

the problem is in routes that con't recognize the controller class.

Route::get('/students', 'StudentController@index')->name('students.index'); 

the StudentController in this route must be determined that what's its namespace.

in App\Providers\RouteServiceProvider file there is a property named $namespace . this must be App\Http\Controllers or any where your controllers are in it.

if you are uisng newer version of laravel there is another syntax to define route :

use App\Http\Controllers\StudentController ;
Route::get('/students', [StudentController::class,'index'])->name('students.index'); 
0
Vishal Kumar On

Here i got the solution. the problem is in the validation its not matching, here is my new updated code public function store(Request $request)

 {
        
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:students,email',
            'phone_number' => 'required',
            'address' => 'required',
            'select_course' => 'required',
            'highest_qualification' => 'required',
            'cv_file' => 'required|file',
        ]);

and here is my students table migration update

public function up(): void
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('phone_number');
            $table->string('address');
            $table->string('select_course');
            $table->string('highest_qualification');
            $table->string('cv_file');
            $table->timestamps();

        });
    }
and here is the register.blade form in which I updated the name of the fields
    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    @vite('resources/css/app.css')
    <title>Document</title>
</head>
<body>
<!-- header -->
<header class="text-gray-600 body-font">
  <div class="container mx-auto flex flex-wrap p-5 flex-col md:flex-row items-center justify-between hidden w-full md:flex md:w-auto md:order-1">
    <a class="flex title-font font-medium items-center text-gray-900 mb-4 md:mb-0">
     <img class="" src="{{asset('/image/codesmith-logo.svg')}}"> 
      
    </a>
    <nav class="md:ml-auto md:mr-auto flex flex-wrap items-center text-base justify-center">
      <a class="mr-10 hover:text-gray-900 font-semibold tracking-wide">Programs </a>
      <a class="mr-10 hover:text-gray-900 font-semibold tracking-wide">Community & Events</a>
      <a class="mr-10 hover:text-gray-900 font-semibold tracking-wide">Outcomes</a>
      <a class="mr-10 hover:text-gray-900 font-semibold tracking-wide">About</a>
      
     
    </nav>
    <a href="/login"><button class="px-5 mr-5 text-xl font-semibold hover:text-gray-900 tracking-wide" >Login</button></a>
    <button class="inline-flex items-center bg-blue-800 border-0 py-4 px-12 focus:outline-none hover:bg-blue-900 rounded text-base mt-4 md:mt-0 text-white rounded-md text-lg">Apply
      <svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" class="w-4 h-4 ml-1" viewBox="0 0 24 24">
        <path d="M5 12h14M12 5l7 7-7 7"></path>
      </svg>
    </button>
  </div>
</header>


<!-- form -->
<div class="w-full md:w-96 md:max-w-full mx-auto">
  <div class="p-6 border border-gray-300 sm:rounded-md">
  <form action="{{route('students.store')}}" method="post" enctype="multipart/form-data">
      @csrf
      <!-- <pre>
        @php
        print_r($errors->all());
        @endphp
     </pre> -->
      <label class="block mb-6">
        <span class="text-gray-700">Name</span>
        <input
          required
          name="name"
          type="text"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
          placeholder="Joe Bloggs"
        />
      </label>
      <label class="block mb-6">
        <span class="text-gray-700">Email address</span>
        <input
          required
          name="email"
          type="email"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
          placeholder="[email protected]"
        />
      </label>
      <label class="block mb-6">
        <span class="text-gray-700">Phone no.</span>
        <input
          required
          name="phone_number"
          type="text"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
          placeholder="+91..."
        />
      </label>
      <label class="block mb-6">
        <span class="text-gray-700">Address</span>
        <input
          required
          name="address"
          type="text"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
          placeholder=""
        />
      </label>
      <label class="block mb-6">
        <span class="text-gray-700">Select course</span>
        <select
          required
          name="select_course"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
        >
          <option></option>
          <option value="" selected="selected" disabled="disabled">-- select one --</option>
     <option value="Front-end">Front-end</option>
     <option value="Back-end">Back-end</option>
     <option value="Full stack">Full stack</option>
     <option value="UI design">UI design</option>
     <option value="UX design">UX design</option>
        </select>
      </label>
        

      <label class="block mb-6">
        <span class="text-gray-700">Highest Qualification</span>
        <select
          required
          name="highest_qualification"
          class="
            block
            w-full
            mt-1
            border-gray-300
            rounded-md
            shadow-sm
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
        >
          <option></option>
          <option value="" selected="selected" disabled="disabled">-- select one --</option>
     <option value="Primary education">Primary education</option>
     <option value="Secondary education">Secondary education or high school</option>
     <option value="Bachelor's degree">Bachelor's degree</option>
     <option value="Master's degree">Master's degree</option>
     <option value="Doctorate or higher">Doctorate or higher</option>
        </select>
      </label>

     
      <label class="block mb-6">
        <span class="text-gray-700">Your CV</span>
        <input
          required
          name="cv_file"
          type="file"
          class="
            block
            w-full
            mt-1
            focus:border-indigo-300
            focus:ring
            focus:ring-indigo-200
            focus:ring-opacity-50
          "
        />
      </label>
      <div class="mb-6">
        <button
          type="submit"
          class="
            h-10
            px-5
            text-indigo-100
            bg-indigo-700
            rounded-lg
            transition-colors
            duration-150
            focus:shadow-outline
            hover:bg-indigo-800
          "
        >
          Submit
        </button>
      </div>
     
          </div>
        </div>
      </div>
      
      <div>
        <div class="mt-2 text-gray-700 text-right text-xs">
          by
          <a href="https://herotofu.com" class="hover:underline" target="_blank"
            >HeroTofu</a
          >
        </div>
      </div>
      
    </form>
  </div>
</div>

</body>
</html>