从具有Laravel关系的多步骤表单插入数据
我有个问题要问你 我有一个多步骤表单:从具有Laravel关系的多步骤表单插入数据,laravel,eloquent,Laravel,Eloquent,我有个问题要问你 我有一个多步骤表单: 贷款(从计算器发送) 创建帐户(电子邮件+密码) 个人资料(名字、姓氏、电话号码等) 地址(街道、城市、州+如果有通信地址等) 雇佣关系(雇主名称、地址等) 完成(发送前检查数据…) 表格中的输入总数为60 我想把它分成更多的表格 使用者 贷款 私人的 地址 部署 虽然我尝试过这种方法,但有些东西告诉我,这种方法即使有效,也不是很安全 因此,我在这里寻求建议和帮助,因为你会这样做吗? Model User.php class User extends Au
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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
Model Loan.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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
Model Personal.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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
模型地址.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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
Model Employment.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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
DB:(用户、贷款、个人、地址、部署
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
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);
}
}
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
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);
}
}
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);
}
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);
}
}
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('/');
}
我不知道我是否正确理解了拉威尔的关系,但我试着
请原谅我的英语,我是斯洛伐克人,我帮过谷歌翻译。如果你想用一个请求做任何事情(构建大型表单并用JS操作),你的存储方法很好 您必须从存储方法中删除
Loan$Loan、User$User、Personal$Personal、address$address、Eployment$Eployment、
,因为您没有为此使用模型路由绑定:
此外,您不需要关联($user->personal()->associate($user);
)或附加所有内容,因为您已经向创建的每个模型添加了用户id
也不要忘记表单验证。也不要忘记表单验证我使用jQuery验证插件在JS中验证表单,但最好在ControllerForm中验证表单。必须在请求文件中验证数据: