【问题标题】:Store two values from Dropdown Option Laravel 5.7存储下拉选项 Laravel 5.7 中的两个值
【发布时间】:2020-02-15 16:11:35
【问题描述】:

我正在使用下拉菜单,它的选项值为 $account->code 但它显示代码和名称

所以我想将代码存储在其列中,并将名称存储在另一列中

这是我的刀片代码:

<div class="form-group col-md-4">
    <label>@lang('site.from_account_number')</label>
    <select class="form-control select2" style="width: 100%;" name="from_account_number" id="from_account_number">
        @foreach ($accounts as $account)
            <option value="{{ $account->code }}">
                {{ $account->code }} - {{ $account->name }} 
            </option>
        @endforeach
    </select>
</div>

这是我的控制器代码:

$depositaction->from_account_number = $request->get('from_account_number');
$record_from_acc_name = Account::find($request->get('from_account_number'));
$depositaction->from_account_name = $record_from_acc_name->name;

但我得到错误说:

ErrorException (E_NOTICE) Trying to get property 'name' of non-object

【问题讨论】:

  • accounts 表的 pk 是什么??

标签: laravel


【解决方案1】:

您应该在代码中查询。请检查以下代码。

$record_from_acc_name = Account::where('code', $request->get('from_account_number'))->first();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多