Open sakshstore opened 3 days ago
Yes, having a student_profiles table to store all the profile information is a good approach. Here's the full code for the model, migration, and controller in Laravel:
Migration Create a migration file for the student_profiles table:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema;
class CreateStudentProfilesTable extends Migration { public function up() { Schema::create('student_profiles', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('middle_name')->nullable(); $table->string('last_name'); $table->date('date_of_birth'); $table->enum('gender', ['male', 'female', 'other']); $table->string('email')->unique(); $table->string('phone_number'); $table->string('street'); $table->string('city'); $table->string('state'); $table->string('postal_code'); $table->string('country'); $table->string('profile_picture')->nullable(); $table->string('preferred_language'); $table->text('special_accommodations')->nullable(); $table->timestamps(); }); }
public function down() { Schema::dropIfExists('student_profiles'); } }
Model Create a model for StudentProfile:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model;
class StudentProfile extends Model { use HasFactory;
protected $fillable = [ 'first_name', 'middle_name', 'last_name', 'date_of_birth', 'gender', 'email', 'phone_number', 'street', 'city', 'state', 'postal_code', 'country', 'profile_picture', 'preferred_language', 'special_accommodations', ]; }
Controller Create a controller for handling student profiles:
namespace App\Http\Controllers;
use App\Models\StudentProfile; use Illuminate\Http\Request;
class StudentProfileController extends Controller { public function index() { $profiles = StudentProfile::all(); return view('student_profiles.index', compact('profiles')); }
public function create() { return view('student_profiles.create'); }
public function store(Request $request) { $validatedData = $request->validate([ 'first_name' => 'required|string|max:255', 'middle_name' => 'nullable|string|max:255', 'last_name' => 'required|string|max:255', 'date_of_birth' => 'required|date', 'gender' => 'required|in:male,female,other', 'email' => 'required|string|email|max:255|unique:student_profiles', 'phone_number' => 'required|string|max:15', 'street' => 'required|string|max:255', 'city' => 'required|string|max:255', 'state' => 'required|string|max:255', 'postal_code' => 'required|string|max:10', 'country' => 'required|string|max:255', 'profile_picture' => 'nullable|string|max:255', 'preferred_language' => 'required|string|max:255', 'special_accommodations' => 'nullable|string', ]);
StudentProfile::create($validatedData);
return redirect()->route('student_profiles.index')->with('success', 'Profile created successfully.'); }
public function show(StudentProfile $studentProfile) { return view('student_profiles.show', compact('studentProfile')); }
public function edit(StudentProfile $studentProfile) { return view('student_profiles.edit', compact('studentProfile')); }
public function update(Request $request, StudentProfile $studentProfile) { $validatedData = $request->validate([ 'first_name' => 'required|string|max:255', 'middle_name' => 'nullable|string|max:255', 'last_name' => 'required|string|max:255', 'date_of_birth' => 'required|date', 'gender' => 'required|in:male,female,other', 'email' => 'required|string|email|max:255|unique:student_profiles,email,' . $studentProfile->id, 'phone_number' => 'required|string|max:15', 'street' => 'required|string|max:255', 'city' => 'required|string|max:255', 'state' => 'required|string|max:255', 'postal_code' => 'required|string|max:10', 'country' => 'required|string|max:255', 'profile_picture' => 'nullable|string|max:255', 'preferred_language' => 'required|string|max:255', 'special_accommodations' => 'nullable|string', ]);
$studentProfile->update($validatedData);
return redirect()->route('student_profiles.index')->with('success', 'Profile updated successfully.'); }
public function destroy(StudentProfile $studentProfile) { $studentProfile->delete();
return redirect()->route('student_profiles.index')->with('success', 'Profile deleted successfully.'); } }
Routes Define routes for the student profile controller in web.php:
use App\Http\Controllers\StudentProfileController;
Route::resource('student_profiles', StudentProfileController::class);
This setup will allow you to manage student profiles with full CRUD operations. Let me know if you need any further adjustments or additional features!
what will be table strucutre for
student , teacher , admin etc plz update