0
votes

I have a question for you.

I have a multisteps form:

  1. Loan (this is sent from the calculator)
  2. Create an account (email + password)
  3. Personal data (first name, last name, phone number etc ...)
  4. Addresses (street, city, state + if have correspondence address etc ...)
  5. Employment (name of employer, address of employer, etc ...)
  6. Finish (Review your data before sending ...)

the total number of inputs that are in the form is 60

I would like to split it into more tables

  1. users
  2. loans
  3. personal
  4. addresses
  5. eployment

While I tried this, but something tells me that this method is not very safe even though it is working.

Therefore, I am turning here for advice and help as you would have done something like this?

Model User.php

class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function loans() {
        return $this->belongsToMany(Loan::class)->withTimestamps();
    }
    public function personal() {
        return $this->belongsTo(Personal::class);
    }
    public function adress() {
        return $this->belongsTo(Adress::class);
    }
    public function employment() {
        return $this->belongsTo(Eployment::class);
    }
}

Model Loan.php

class Loan extends Model
{
    protected $hidden = ['amount', 'month', 'payment'];
    public function users() {
        return $this->belongsToMany(User::class)->withTimestamps();
    }
}

Model Personal.php

class Personal extends Model
{
    protected $fillable = [
        'first_name', 'last_name', 'identification_number', 'identity_card_number', 'date_of_birth', 'phone'
    ];
    public function users() {
        return $this->hasMany(User::class);
    }
}

Model Adress.php

protected $fillable = [
        'adress', 'adress_number', 'city', 'postcode', 'country', 'correspond_adress', 'correspond_adress_number', 'correspond_city', 'correspond_postcode', 'correspond_country', 'type_of_housing', 'since_year', 'marital_status', 'number_of_kids'
    ];
    public function users() {
        return $this->hasMany(User::class);
    }

Model Employment.php

class Eployment extends Model
{
    protected $fillable = [
        'type_of_occupation', 'client_ico', 'client_dic', 'employer_name', 'employer_ico', 'employment_adress', 'employment_city', 'month_of_arrival', 'year_of_arrival', 'net_monthly_income', 'other_income', 'payment_method', 'expenditure_payments', 'loan_repayments', 'wage_deductions', 'other_expenditure', 'have_bank_account', 'iban_account'
    ];
    public function users() {
        return $this->hasMany(User::class);
    }
}

DB: (users, loans, personal, adresses, eployment

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Schema::create('loans', function (Blueprint $table) {
    $table->id();
    $table->string('amount');
    $table->string('month');
    $table->string('payment');
    $table->timestamps();
});

Schema::create('loan_user', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('loan_id');
    $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
    $table->foreign('loan_id')->references('id')->on('loans')->onUpdate('cascade')->onDelete('cascade');
    $table->timestamps();
});

Schema::create('adresses', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->string('adress');
    $table->string('adress_number');
    $table->string('city');
    $table->string('postcode');
    $table->string('country');
    $table->string('correspond_adress')->nullable();
    $table->string('correspond_adress_number')->nullable();
    $table->string('correspond_city')->nullable();
    $table->string('correspond_postcode')->nullable();
    $table->string('correspond_country')->nullable();
    $table->string('type_of_housing');
    $table->string('since_year');
    $table->string('marital_status');
    $table->string('number_of_kids');
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});

Schema::create('eployments', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('user_id');
    $table->string('type_of_occupation');
    $table->string('client_ico')->nullable();
    $table->string('client_dic')->nullable();
    $table->string('employer_name')->nullable();
    $table->string('employer_ico')->nullable();
    $table->string('employment_adress')->nullable();
    $table->string('employment_city')->nullable();
    $table->string('month_of_arrival')->nullable();
    $table->string('year_of_arrival')->nullable();
    $table->string('net_monthly_income');
    $table->string('other_income')->nullable();
    $table->string('payment_method');
    $table->string('expenditure_payments');
    $table->string('loan_repayments')->nullable();
    $table->string('wage_deductions')->nullable();
    $table->string('other_expenditure')->nullable();
    $table->string('have_bank_account');
    $table->string('iban_account');
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});

LoanController.php

public function store(Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, Request $request)
    {
        $user = User::create([
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        $data = new Loan;
        $data->amount = $request->amount;
        $data->month = $request->month;
        $data->payment = $request->payment;

        $personal = new Personal;
        $personal->user_id = $user->id;
        $personal->first_name = $request->first_name;
        $personal->last_name = $request->last_name;
        $personal->identification_number = $request->identification_number;
        $personal->identity_card_number = $request->identity_card_number;
        $personal->date_of_birth = $request->date_of_birth;
        $personal->phone = $request->phone;

        $adress = new Adress;
        $adress->user_id = $user->id;
        $adress->adress = $request->adress;
        $adress->adress_number = $request->adress_number;
        $adress->city = $request->city;
        $adress->postcode = $request->postcode;
        $adress->country = $request->country;
        $adress->correspond_adress = $request->correspond_adress;
        $adress->correspond_adress_number = $request->correspond_adress_number;
        $adress->correspond_city = $request->correspond_city;
        $adress->correspond_postcode = $request->correspond_postcode;
        $adress->correspond_country = $request->correspond_country;
        $adress->type_of_housing = $request->type_of_housing;
        $adress->since_year = $request->since_year;
        $adress->marital_status = $request->marital_status;
        $adress->number_of_kids = $request->number_of_kids;

        $eployment = new Eployment;
        $eployment->user_id = $user->id;
        $eployment->type_of_occupation = $request->type_of_occupation;
        $eployment->client_ico = $request->client_ico;
        $eployment->client_dic = $request->client_dic;
        $eployment->employer_name = $request->employer_name;
        $eployment->employer_ico = $request->employer_ico;
        $eployment->employment_adress = $request->employment_adress;
        $eployment->employment_city = $request->employment_city;
        $eployment->month_of_arrival = $request->month_of_arrival;
        $eployment->year_of_arrival = $request->year_of_arrival;
        $eployment->net_monthly_income = $request->net_monthly_income;
        $eployment->other_income = $request->other_income;
        $eployment->payment_method = $request->payment_method;
        $eployment->expenditure_payments = $request->expenditure_payments;
        $eployment->loan_repayments = $request->loan_repayments;
        $eployment->wage_deductions = $request->wage_deductions;
        $eployment->other_expenditure = $request->other_expenditure;
        $eployment->have_bank_account = $request->have_bank_account;
        $eployment->iban_account = $request->iban_account;

        $data->save();

        $user->personal()->associate($user);
        $personal->save();

        $user->adress()->associate($user);
        $adress->save();

        $user->eployment()->associate($user);
        $eployment->save();

        $user->loans()->attach($data);

        return redirect('/');
    }

I don't know if I understood Laravel Relationships correctly but I try to ...

Excuse my English I'm Slovak and I helped with Google Translator

1

1 Answers

0
votes

Well if you want to do everything with one request (building large form and manipulating it with JS) your store method is quite good.

You have to remove Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, from your store method because your are not using model route binding for this: https://laravel.com/docs/5.8/routing#route-model-binding.

Also you don't need to associate ($user->personal()->associate($user);) or attach everything because you already adding user_id to every model you create.

Also don't forget form validation.