在Laravel数据库表的不同列中存储来自多个字段的用户输入。

huangapple go评论63阅读模式
英文:

Storing user input from multiple fields in different columns of Laravel database table

问题

Sure, here is the code to store and update the additional data in different columns of the 'users' table in your DashboardController.php file:

public function withdrawalConfigure()
{
    if ($this->request->type != 'paypal' && $this->request->type != 'bank') {
        return redirect('user/dashboard/withdrawals/configure')->withError(__('misc.error'));
    }

    if ($this->request->type == 'paypal') {
        // Validate and store PayPal data
        $rules = [
            'email_paypal' => 'required|email|confirmed',
        ];

        $this->validate($this->request, $rules);

        $user = User::find(auth()->id());
        $user->paypal_account = $this->request->email_paypal;
        $user->payment_gateway = 'PayPal';
        $user->save();

        return redirect('user/dashboard/withdrawals/configure')->withSuccess(__('admin.success_update'));
    } elseif ($this->request->type == 'bank') {
        // Validate and store Bank data
        $rules = [
            'bank' => 'required',
            'bank_code' => 'required',
            'branch_code' => 'required',
            'ac_num' => 'required',
            'ac_name' => 'required',
        ];

        $this->validate($this->request, $rules);

        $user = User::find(auth()->id());
        $user->bank = $this->request->bank;
        $user->bank_code = $this->request->bank_code;
        $user->branch_code = $this->request->branch_code;
        $user->ac_num = $this->request->ac_num;
        $user->ac_name = $this->request->ac_name;
        $user->payment_gateway = 'Bank';
        $user->save();

        return redirect('user/dashboard/withdrawals/configure')->withSuccess(__('admin.success_update'));
    }
}

This code will validate and store the additional bank-related data in separate columns in the 'users' table when the user types and saves it on the payouts configuration page.

英文:

I have a iage selling and buying multi vendor site like shutterstock and it was developed with laravel php. And there is a page called payouts and it refers to the withdrawals-configure.blade.php. However when the user input their bank details into the input field and save it will be stored at the database ‘users’ table ‘bank’ column.

Now I have added more input fields to the page and they are ‘Bank Code’ , ‘Branch Code’ , ‘Account Number’ and ‘ Account Name’ I have written the code for these inputs in withdrawal-configure.blade.php file but the function that i have written in DashboardController.php is not storing data in database.

I have a image selling and buying multi vendor site like shutterstock and it was developed with laravel php. And there is a page called payouts and it refers to the withdrawals-configure.blade.php. However when the user input their bank details into the input field and save it will be stored at the database ‘users’ table ‘bank’ column.

Here’s the code for that process:

<!-- ***** FORM ***** -->
<form action="{{ url('user/withdrawals/configure/bank') }}" method="post" name="form" class="mb-5">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-floating mb-3"> <!-- Bank Name -->
<textarea class="form-control" placeholder="{{ trans('misc.bank_details') }}" name="bank" id="input-bank_details" style="height: 100px">{{ auth()->user()->bank }}</textarea>
<label for="input-bank_details">{{ trans('misc.bank_details') }}</label>
</div>
<button type="submit" class="btn w-100 btn-lg btn-custom">{{ trans('misc.save_payout_method') }}</button>
</form><!-- ***** END FORM ***** -->

Now I have added more input fields to the page and they are ‘Bank Code’ , ‘Branch Code’ , ‘Account Numbe’ and ‘ Account Name’ I have written the code for these inputs in the withdrawal-configure.blade.php file:

<div class="form-floating mb-3"> <!-- Bank Code -->
<textarea class="form-control" placeholder="{{ trans('misc.bank_details') }}" name="bank_code" id="input-bank_code" style="height: 100px" maxlength="4" size="4">{{ auth()->user()->bank_code }}</textarea>
<label for="input-bank_code">Bank Code</label> 
</div>
<div class="form-floating mb-3"> <!-- Branch Code -->
<textarea class="form-control" placeholder="{{ trans('misc.bank_details') }}" name="branch_code" id="input-branch_code" style="height: 100px" maxlength="4" size="4">{{ auth()->user()->branch_code }}</textarea>
<label for="input-branch_code">Branch Code</label>
</div>
<div class="form-floating mb-3"> <!-- Account Number -->
<textarea class="form-control" placeholder="{{ trans('misc.bank_details') }}" name="ac_num" id="input-ac_num" style="height: 100px">{{ auth()->user()->ac_num }}</textarea>
<label for="input-ac_num">Account Number</label>
</div>
<div class="form-floating mb-3"> <!-- Account Name -->
<textarea class="form-control" placeholder="{{ trans('misc.bank_details') }}" name="ac_name" id="input-ac_name" style="height: 100px">{{ auth()->user()->ac_name }}</textarea>
<label for="input-ac_name">Account Name</label>
</div>           

And Now I want to program to store the data in different columns in the ‘users’ table when user types and saves in the other input fields. However I have attached the regarding function this on DashboardController.php

    public function withdrawalConfigure()
{
if ($this->request->type != 'paypal' && $this->request->type != 'bank') {
return redirect('user/dashboard/withdrawals/configure')->withError(__('misc.error'));
}
// Validate Email Paypal
if ($this->request->type == 'paypal') {
$rules = [
'email_paypal'  => 'required|email|confirmed',
];
$this->validate($this->request, $rules);
$user = User::find(auth()->id());
$user->paypal_account = $this->request->email_paypal;
$user->payment_gateway = 'PayPal';
$user->save();
return redirect('user/dashboard/withdrawals/configure')->withSuccess(__('admin.success_update'));
} else if ($this->request->type == 'bank') {
$rules = [
'bank' => 'required',
];
$this->validate($this->request, $rules);
$user = User::find(auth()->id());
$user->bank = $this->request->bank;
$user->payment_gateway = 'Bank';
$user->save();
return redirect('user/dashboard/withdrawals/configure')->withSuccess(__('admin.success_update'));
}
}//<--- End Method

Can you help me to write the code to store and update these data on DB on DashboardController.php file please

答案1

得分: 1

  1. 新的表单字段需要在字段中。
  2. 你可能需要将新字段添加到用户模型中的fillable数组。
  3. 你需要将你想要的新字段添加到规则中。
  4. 在保存模型之前,你需要将新的表单字段添加到$user->column_name = $this->request->field_name,例如:$user->bank_code = $this->request->bank_code

此外,由于在if语句中已经有早期返回,所以不需要那个else语句,而第二种情况可以只是一个if语句。最后,在两个if语句之后,你可以返回null。

英文:
  1. The new form fields need to be in the field.
  2. You may need to add the new fields to the fillable array in the User model.
  3. You need to add the new fields you want to the rules.
  4. You add the new form fields to the $user->column_name = $this->request->field_name before you save the model. ex: $user->bank_code = $this->request->bank_code

You also do not need that else in there since you have an early return in the if and the second case can just be an if. And lastly, you can return null after both if statements.

huangapple
  • 本文由 发表于 2023年7月13日 13:49:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定