【问题标题】:How to fetch price through another table where $product->name is equal to $order->product如何通过 $product->name 等于 $order->product 的另一个表获取价格
【发布时间】:2021-07-12 22:20:24
【问题描述】:

我有 2 个表产品和订单,我想通过产品表获取价格 这是我的表格

<div id="TextBoxDiv1">
     <input type="hidden" value="{{Auth::user()->name}}" name="order_by">
     <input class="text email" type="text" name="quantity[]" placeholder="Quantity" required="" id='textbox1' style="width: 230px; height: 41px; margin-top: 10px">
     <div class="form-group row">
     {{-- <label class="col-md-3 mt-3">Single Select</label> --}}
     <div class="col-md-9">
     <select class="select2 form-select shadow-none" style="width: 230px; height: 41px; margin-top: 10px" name="product[]">
             <option>Select Product</option>
             @foreach ($products as $product)
                   <option value="{{ $product->name }}">{{ $product->name }}</option>
              @endforeach
      </select>
    </div>
    </div>
 </div>

这是我的 OrderController,我不明白如何获取价格,因为我们从表单中获取的值在数组中,我想将其转换为字符串,并通过该名称通过产品表获取价格

public function store(Request $request)
{
    $products = Product::all();
    $request->validate([
        'order_by' => 'required',
        'product' => 'required',
        'quantity' => 'required',
        'status' => 'sometimes',
        'price' => 'sometimes',
    ]);

    $order = new Order();
    $order->product = implode(',', $request->product);
    $order->quantity = implode(',', $request->quantity);
    // $order->price = ;
    $order->order_by = $request->order_by;
    $order->status = $request->status;
    $order->save();
    return redirect()->route('user/pending', compact('products'))->withSuccess('Done');
}

这是订单表的图片

这是产品表的图片

【问题讨论】:

  • 提供您的订单和产品型号代码。
  • @Vega OP 没有得到关于这个问题的答案,但他得到了关于其他问题的答案,所以我投票决定关闭它......但最终 OP 得到了他的问题的解决方案,所以现在它没有没关系...当然我会转移我的投票:)

标签: php laravel laravel-8


【解决方案1】:

将option的值设置为$product->id,然后在controller中获取价格

在刀片中:

@foreach ($products as $product)
   <option value="{{ $product->id }}">{{ $product->name }}</option>
@endforeach

在控制器中:

$product = Product::find(implode(',', $request->product));

$order->product = $product->name;
$order->price =$product->price;

【讨论】:

    猜你喜欢
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 1970-01-01
    • 2021-11-23
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多