I am using Laravel 8 and trying to get an application form to post to two tables in my database
From my 2 database migration files:
public function up() { Schema::create('applicants', function (Blueprint $table) { $table->id(); $table->string('apptitle'); $table->string('firstname'); $table->string('middlename')->nullable(); ... $table->timestamps(); }); }
public function up() { Schema::create('applications', function (Blueprint $table) { $table->id(); $table->integer('applicant_id'); $table->integer('user_id'); $table->integer('loanAmount'); $table->string('loanTerm'); ... $table->timestamps(); }); }
Models:
class Applicant extends Model { use HasFactory; protected $table = 'applicants'; protected $fillable = [ 'apptitle', 'firstname', 'middlename'... ]; public function application() { return $this->hasOne(Application::class); } }
class Application extends Model { use HasFactory; protected $table = 'applications'; protected $fillable = [ 'applicant_id', 'user_id', 'loanAmount', 'loanTerm', ... ]; public function applicant() { return $this->belongsTo(Applicant::class); } }
Controllers:
namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpRequestsApplicantsCreateApplicantRequest; class ApplicantsController extends Controller { ... public function store(CreateApplicantRequest $request) { $applicant = Applicant::create([ 'apptitle' => $request->apptitle, 'firstname' => $request->firstname, 'middlename' => $request->middlename, ... ]); }
namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsApplication; use AppModelsApplicant; use AppModelsUser; use AppHttpRequestsApplicationsCreateApplicationRequest; class ApplicationsController extends Controller { ... public function store(CreateApplicationRequest $request) { $application = Application::create([ 'applicant_id' => $request->applicant_id, 'user_id' => 'required', 'loanAmount' => 'required', 'loanTerm' => 'required', ... ]); } }
Requests:
public function rules() { return [ 'apptitle' => 'required', 'firstname' => 'required', 'middlename', ... ]; }
public function rules() { return [ 'applicant_id' => 'required', 'user_id' => 'required', 'loanAmount' => 'required', 'loanTerm' => 'required', ... ]; }
web.php
Route::get('applicants','<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15546565797c76747b6166567a7b61677a797970675566617a6770">[email protected]</a>'); Route::resource('applications', 'ApplicationsController'); Route::get('applications/{application}', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="074677776b6e6466736e6869744468697375686b6b627547746f6870">[email protected]</a>');
I am continually getting errors: The applicant id field is required. (If I make this field nullable the form does successfully post all other fields to the database.)
This is my first big Laravel project so any help would be greatly appreciated.
Update:
I have gone through the answers provided and am still getting the same error.
I feel the main issue is – when the form is filled out the applicant_id field for the newly created Applicant is not being captured and added to the applications table?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can store data from one form into 2 tables like this.
Method 2
Remove use AppHttpRequestsApplicantsCreateApplicantRequest; from your ApplicationsController and run the following cmd commands:
composer dump-autoload php artisan cache:clear php artisan config:clear php artisan view:clear php artisan route:clear
These commands clear all cache from your project.
Add nullable to your application migration applicant_id:
$table->integer('applicant_id')->nullable();
Method 3
I finally was able to get my form posting correctly to both databases – a big thank you to all those that have helped me in this journey.
This is my updated store function in my ApplicationsController:
public function store(CreateApplicationRequest $request, Applicant $applicant) { $applicant = Applicant::create([ 'apptitle' => $request->apptitle, 'firstname' => $request->firstname, 'middlename' => $request->middlename, ... ]); $application = $applicant->application()->create([ 'applicant_id' => $applicant->id, 'user_id' => auth()->id(), 'loanAmount' => $request->loanAmount, 'loanTerm' => $request->loanTerm, ... ]); // redirect the user return redirect(route('applications.index')); }
I hope this answer helps someone else out!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0