【问题标题】:Insert data from multistep form with Laravel Relationships使用 Laravel 关系从多步表单中插入数据
【发布时间】:2020-03-11 18:37:27
【问题描述】:

我有一个问题要问你。

我有一个多步骤表单:

  1. 贷款(从计算器发送)
  2. 创建一个帐户(电子邮件 + 密码)
  3. 个人数据(名字、姓氏、电话号码等...)
  4. 地址(街道、城市、州 + 如果有通信地址等...)
  5. 就业(雇主名称、雇主地址等...)
  6. 完成(发送前检查您的数据...)

表单中的输入总数为 60

我想把它拆分成更多的表格

  1. 用户
  2. 贷款
  3. 个人的
  4. 地址
  5. 就业

虽然我尝试了这个,但有些东西告诉我,即使它有效,这种方法也不是很安全。

因此,我转向这里寻求建议和帮助,因为您会这样做吗?

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);
    }
}

型号地址.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);
    }

模特就业.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);
    }
}

数据库:(用户、贷款、个人、地址、就业

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('/');
    }

我不知道我是否正确理解 Laravel 关系,但我尝试...

对不起,我的英语是斯洛伐克语,我帮助过 Google 翻译

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    如果你想用一个请求做所有事情(构建大型表单并用 JS 操作它)你的 store 方法非常好。

    您必须从 store 方法中删除 Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment,,因为您没有为此使用模型路由绑定:https://laravel.com/docs/5.8/routing#route-model-binding

    您也不需要关联 ($user->personal()->associate($user);) 或附加所有内容,因为您已经将 user_id 添加到您创建的每个模型中。

    也不要忘记表单验证。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-30
    • 1970-01-01
    • 2016-10-05
    • 2023-03-13
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多