如何在Laravel 5.3 Combobox中的名称中插入id

时间:2017-02-12 06:00:22

标签: laravel laravel-5.3

我想通过从组合框中选择供应商的名称来插入供应商的ID。

查看

<label for="supplier">Supplier</label>
<input list="supplier" name="supplier" placeholder="Select Supplier" class="form-control">
@foreach($suppliers as $key=>$value)
    <datalist id="supplier">
        <option value="{{$key}}">{{$value}}
    </datalist>
@endforeach 

控制器

<?php

public function store(Request $request, User $user)
{
    $user = Auth::user();
    $product = new Product;
    $request->user()->products()->create($request->all());
}

模型

protected $fillable = ['name', 'qty', 'bprice', 'sprice', 'edate'];

public function user()
{
    return $this->belongsTo(User::class, user_id);
}

1 个答案:

答案 0 :(得分:0)

使用此

  public function store(Request $request, User $user)
{
    $user = Auth::user();

    $product = new Product;
    $product->supplier_id = $request->supplier;
    $product->price = $request->price; // if u hv it

。 //完成剩下的工作

   $user->products()->save($product);

   // finish the rest

    }