function in controller
public function submitStudent(CreateStudentRequest $request): RedirectResponse
{
$user = auth()->user();
Student::create([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'balances' => []
]);
return redirect()->route('parent.create-student_submit', ['user_id' => auth()->user()->id]);
}
custom request
class CreateStudentRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'last_name' => 'required',
'first_name' => 'required',
'country' => 'required',
'street_address' => 'required',
'city' => 'required',
'state' => 'required',
'zip' => 'required',
];
}
}
ROUTE
Route::post('/parent/create-student/{user_id}', [ParentController::class, 'submitStudent'])->name('parent.create-student_submit');
VIEW
<form id="form" method="POST" action="{{route('parent.create-student_submit', ['user_id' => $user_id])}}" class="mt-8 space-y-6 w-full">
@csrf
<div class="bg-white p-8">
<div>
<h3 class="text-lg font-medium leading-6 text-gray-900">Personal Information</h3>
<p class="mt-1 text-sm text-gray-500">Use a permanent address where you can receive mail.</p>
</div>
<div class="mt-6 grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-6">
<div class="sm:col-span-2">
<label for="last-name" class="block text-sm font-medium text-gray-700">Last name</label>
<div class="mt-1">
<input type="text" value="{{old('last_name')}}" required name="last_name" id="last_name" autocomplete="given-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
</div>
</div>
<div class="sm:col-span-2">
<label for="first-name" class="block text-sm font-medium text-gray-700">First name</label>
<div class="mt-1">
<input type="text" value="{{old('first_name')}}" required name="first_name" id="first_name" autocomplete="family-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
</div>
</div>
<div class="sm:col-span-2">
<label for="middle-name" class="block text-sm font-medium text-gray-700">Middle name</label>
<div class="mt-1">
<input type="text" value="{{old('middle_name')}}" name="middle_name" id="middle_name" autocomplete="additional-name" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm">
</div>
</div>
(i did not copied all view, because i dont think something iis in view, just to showcase)
! MIGRATION (user)!
return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('first_name')->default('unfilled');
$table->string('last_name')->default('unfilled');
$table->string('middle_name')->nullable();
$table->string('email')->unique();
$table->string('password');
$table->foreignId('school_id')->default(1)->constrained();
$table->integer('billingo_id')->default(0);
$table->tinyInteger('summary_frequency')->default(0);
$table->tinyInteger('finished_onboarding')->default(0);
$table->json('user_information')->nullable();
$table->string('two_factor_token')->nullable()->unique();
$table->boolean('is_verified')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
};
!! MIGRATION !! (student)
return new class extends Migration
{
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name')->nullable();
$table->string('card_number')->nullable();
$table->string('card_data')->nullable();
$table->json('user_information')->nullable();
$table->json('balances');
$table->foreignId('user_id')->constrained();
$table->foreignId('school_id')->constrained('users','school_id');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('students');
}
};
User model :
protected $guarded = ['id'];
protected $hidden = [
'password',
'remember_token',
];
public function school(): BelongsTo
{
return $this->belongsTo(School::class);
}
public function students(): HasMany
{
return $this->hasMany(Student::class);
}
Student model :
class Student extends Model
{
use HasFactory;
protected $guarded = ['id'];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function school(): BelongsTo
{
return $this->belongsTo(School::class);
}
public function transactions(): HasMany
{
return $this->hasMany(Transaction::class);
}
}