如何在Laravel中从组合选择显示数据到文本字段

时间:2019-07-12 03:17:41

标签: laravel

Display data to textfield based combo selection dynamicaly

我想根据laravel 5.8中的组合框/下拉框选择将数据显示到文本字段。然后保存数据。有完整的教程如何从模型,视图和控制器做到这一点。

在客户控制器中

    public function create()
    {
        $customer= Customer::all();
        return view('Customer.create', compact('Customer'));
    }

在视图create.blade中

     <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>CUSTOMER</strong>
                <select type="text" class="form-control" name="kde_cust" id="cde_customer" required>
                    <option value=""> -- PICK CUST-- </option>
                    @foreach($cust as $cust)
                        <option value="{{ $cust->cde_cust }}" selected>{{ $cust->nme_customer }}</option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>NAME</strong>
                <input type="text" name="cust_name" id="cust_name" class="form-control" value="{{ $cust->nme_customer }}">
            </div>
        </div>

1 个答案:

答案 0 :(得分:0)

这应该有所帮助:

创建路由以获取用户地址:

Route::get('user-address/{user_id}','YourController@method')->name('user.address');

您的控制器方法应如下所示:

public function yourFunction($cde_customer)
{
    $customer = Customer::where('cde_customer', $cde_customer)->first();
    $address = $customer->address;
    return response()->json([
       'address' => $address,
    ]);
}

将此脚本添加到刀片中:

$("#cde_customer").on('change', function(){
    var user_id = $(this).val();
    $.ajax({
        url: '/user-address/' + user_id,
        method: 'GET',
        success: function(response){
            $("#textfield").html(response.address);
        }
    });
});

希望有帮助。