laravel 我要将产品及表单上载到数据库

bzzcjhmw  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(143)

第一个
我不能上传到数据库。它说
错误提示-页面未找到抱歉,你所请求的页面不存在!
我想从形式上传到数据库。在我使用产品数据库与播种机

ecfsfe2w

ecfsfe2w1#

更新控制器upload()函数:

use Illuminate\Http\Request;
public function upload(Request $request){
    $request->validate([
         'file' => 'required|mimes:csv,txt,,xlx,xls,pdf|max:2048'
    ]);
    $products = new Product;
    $products->name = $request->name;
    $products->price = $request->price;

    if ($request->file('file')) {
        $fileName = time().'.'.$request->file->extension();
        $request->file->move(public_path('/upload'), $fileName);
        $products->file= $filename;
    } 

    $products->save(); 
    return redirect('/');
}

在此public_path('/upload')路径中,您可以使用自己的路径将文件上载到其中。
您必须将此行写在插入查询$products->save();之后,否则无法将其存储在数据库中您还将文件上载路径写在$file->move('uploads/', $filename);上,您无法定义文件上载路径将是file->move(public_path('/your file path'), $fileName);
现在,应该能正常工作了.

jv4diomz

jv4diomz2#

您的观点

<form method="POST" action="/upload" 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>

您的控制器

public function upload(Request $request)
{
$products = new Product;
$products->name = $request->name;
$products->price = $request->price;
if ($request->hasFile('file')) {
        $file = $request->file('file');
        $extension = $file->getClientOriginalExtension();
        $filename = time() . '.' . $extension;
        $file->move('uploads/', $filename);
        $products->file= $filename;
    }
$products->save();
return redirect('/');

}
那应该有用

gcuhipw9

gcuhipw93#

更新控制器中的代码
使用照明\Http\请求;

public function upload(Request $request){
    $products = new Product;
    $products->name = $request->name;
    $products->price = $request->price;

    if ($request->file('file')) {
        $file = $request->file('file');
        $path = public_path() . '/uploads';
        $filename = time() . '_' . $file->getClientOriginalName();
        if ($file->move($path, $filename)) {
            $products->file= $filename;
        } 
    } 

    $products->save(); 
    return redirect('/');
}

在路径部分中,您可以定义文件路径,您要上传文件的位置

相关问题