【问题标题】:I want to upload product with form to database我想将带有表格的产品上传到数据库
【发布时间】:2022-11-04 15:42:55
【问题描述】:
@extends('master') @section("content") <h1>Upload</h1> <form action="upload" method="POST" enctype="multipart/form-data">
    @csrf
        <div class="form-group">
        <label>Name</label>
        <input type="name" name="name" class="form-control" placeholder="Enter Name">
        </div>
        <div class="form-group">
        <label>Price</label>
        <input type="price" name="price" class="form-control" placeholder="Enter price">
        </div>
        <label>Category</label>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Choose
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">Item</a>
              <br><a class="dropdown-item" href="#">Diamond</a>
              <br><a class="dropdown-item" href="#">Akun</a>
            </div>
          </div>
        </div>
        <div>
        <input type="file" name="file"><br><br></div>
        <div>
        <button type="submit"> Sell Item</button>
        </div> </form> @endsection ```

function upload(Request $req)
{
    
    $products = new Product;
    $products->name=$req->name;
    $products->price=$req->price;
    $products = $req->file('file')->store('Products');
    $req->product()->upload([
        'file'=> $products
    ]);
    return redirect ('/');

} ```

我无法将其上传到数据库。它说

坏方法调用异常 方法 Illuminate\Http\Request::product 不存在。

我想从表单上传到数据库。在我使用带有播种机的产品数据库之前

【问题讨论】:

  • 将 $req 更改为 $request 然后检查。
  • 但我做了这个功能上传(请求$req)
  • 没有 Stack Overflow 问题需要以“hello”开头;而“任何人都可以帮助我”是不必要的邀请。
  • 您不会“上传”到数据库,而是保存到数据库$product-&gt;save()。我建议您阅读文档。

标签: php mysql laravel


【解决方案1】:

如果您在表单中使用$req-&gt;product(),则对您的产品没有价值。您需要为您的实例产品设置值并使用$product-&gt;save();,但您不能使用$req-&gt;product();

【讨论】:

    猜你喜欢
    • 2015-03-19
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    相关资源
    最近更新 更多