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 AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
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 AppHttpControllersStudentController;
use IlluminateSupportFacadesRoute;
use AppHttpControllersCustomAuthController;
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 AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsStudent;
use AppModelsCourse;
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 IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
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
Answers
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)
and here is my students table migration update
the problem is in routes that con’t recognize the controller class.
the
StudentController
in this route must be determined that what’s its namespace.in
AppProvidersRouteServiceProvider
file there is a property named$namespace
. this must beAppHttpControllers
or any where your controllers are in it.if you are uisng newer version of laravel there is another syntax to define route :