Laravel store()方法不会提交数据

时间:2018-11-23 08:57:53

标签: php laravel laravel-blade

我是Laravel的新手,如果这是一个愚蠢的问题,请原谅我。

我正在尝试创建一个创建银行帐户的表单,并且我有两个名为Team和AgentBank的模型。我的团队模型与AgentBank模型具有一对多的雄辩关系。

下面的表格从Team模型获取数据以提供“ team_id”输入。

agent-bank / create.blade.php

<div class="col-3">
    <div class="dark-panel card p-3">
        <form method="post" action="{{ route('agent-bank.store') }}">
            @csrf
            <div class="form-group mb-2">
                <select class="form-control" name="team_id">
                    <option value="" selected disabled>Select Team</option>
                    @foreach($teams as $team)
                        <option value={{ $team->id }}>{{ $team->team_name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="form-group mb-2">
                <select class="form-control">
                    <option value="" selected disabled>Select Bank</option>
                    <option value="bank1">BANK1</option>
                    <option value="bank2">BANK2</option>
                    <option value="bank3">BANK3</option>
                    <option value="bank4">BANK4</option>
                    <option value="bank5">BANK5</option>
                </select>
            </div>

            <div class="form-group mb-2">
                <input class="form-control" name="account_name" type="text" placeholder="Account Name">
            </div>

            <div class="form-group mb-2">
                <input class="form-control" name="account_number" type="text" placeholder="Account Number">
            </div>

            <div class="form-group mb-2">
                <select class="form-control" name="account_type">
                    <option value="" selected disabled>Select Function</option>
                    <option value="func1">FUNC1</option>
                    <option value="func2">FUNC2</option>
                    <option value="func3">FUNC3</option>
                    <option value="func4">FUNC4</option>
                    <option value="func5">FUNC5</option>
                </select>
            </div>

            <div class="form-group mb-2">
                <input class="form-control" name="account_balance" type="text" placeholder="Account Balance">
            </div>

            <button type="submit" class="btn primary-button btn-sm mt-2">Submit</button>
        </form>
    </div>
</div>

控制器

public function store(Request $request)
{
    $this->validate($request, [
        'bank_name' => 'required',
        'account_name' => 'required',
        'account_number' => 'required|unique:agent_banks',
        'account_type' => 'required'
    ]);

    $data = $request->except('account_balance');
    $data->account_balance = intval($request->account_balance);

    $agentbank->fill($data)->save();

    return redirect()->route('agent-bank.index')->with('flash_message', 'Bank ' . $agentbank->bank_name . ' ' . $agentbank->account_name . ' ' . $agentbank->account_number . ' succesfully added to ' . $teams->team_name);
}

此方法中是否可能存在拼写错误或错误的代码,导致无法提交?

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为是name的问题。在选择属性中添加name="bank_name"并尝试:

               <div class="form-group mb-2">
                    <select class="form-control" name="bank_name">
                        <option value="" selected disabled>Select Bank</option>
                        <option value="bank1">BANK1</option>
                        <option value="bank2">BANK2</option>
                        <option value="bank3">BANK3</option>
                        <option value="bank4">BANK4</option>
                        <option value="bank5">BANK5</option>
                    </select>
                </div>

答案 1 :(得分:0)

对于第二个问题,请将// On Change Method onFileChange(evt: any) { if (evt.target.files && evt.target.files[0]) { const file = evt.target.files[0]; const reader = new FileReader(); reader.onload = () => { this.form.get('csv').setValue(file); reader.readAsText(evt.target.files[0]); this.uploadData.append('csv', this.form.get('csv').value); }; } } 的输入类型更改为数字。

account_balance