php - 如何从laravel route post将数据保存到mysql数据库中
问题描述
我有 web.php 文件
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
Route::get('/', function () {
return view('welcome');
});
Route::post('/upload', function (Request $request) {
$name=$request->file("thing")->getClientOriginalName();
Storage::disk("google")->putFileAs("",$request->file("thing"),$name);
})->name("upload");
?>
和HTML代码
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Google drive integration in laravel</title>
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12">
<br><br><br>
<form action="/upload" method="post" enctype="multipart/form-data">
@csrf
<input type="file" class="form-control" name="thing" id="title">
<p id="s"></p>
<br>
<input type="submit" class="btn btn-sm btn-block btn-danger" value="Upload">
</form>
</div>
</div>
</div>
</body>
</html>
我希望将 $name 变量中接收到的数据Route::post
保存在数据库中。就像我有 MySQL 数据库名称“registration”和表“books”,我想将此值保存在“URL”列中,我该怎么做?
解决方案
首先移动文件,然后保存记录:
Route::post('/upload', function (Request $request, Table $table) {
$name=$request->file("thing")->getClientOriginalName();
$request->file->move('dirname', $name);
$table->column_name = "dirname/{$name}";
$table->save();
})->name("upload");
推荐阅读
- safari - Safari Web Extension icon does not open popup from the overflow (>>) menu
- pine-script - Tradingview策略,当输入相反方向的订单时,许多订单正在退出
- c - 设置 X11 根窗口像素图
- opengl - gl_Position.z 在动画网格的片段着色器中使用时似乎不正确
- scala - 可以使用多态、可变参数抽象方法制作 Scala 特征吗?
- python - 保存文件的路径(Python)
- oracle - 将序列添加到现有表
- html - 按钮颜色和边框半径在手机上发生变化
- python - 有人可以从 Kivy WebView 本地主机服务器提供一些代码吗?
- android - 如何获取 BottomSheetDialogFragment 中的 viewpager2 的引用?