Php 如何从laravel route post将数据保存到mysql数据库

Php 如何从laravel route post将数据保存到mysql数据库,php,mysql,laravel,Php,Mysql,Laravel,我有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"

我有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>

laravel中的Google驱动集成



@csrf


我希望在
Route::post
中以$name变量接收的数据保存在数据库中。 比如,如果我的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");

首先检查数据库表是否存在

Schema::connection('mysql')->hasTable('books')
如果存在,则返回true,否则返回false

用于插入数据库

  DB::table('books')->insert(['URL'=>$name])
最终代码将是

$tableExist=Schema::connection('mysql')->hasTable('books');
if($tableExist){
 DB::table('books')->insert(['URL'=>$name]);
}
您还可以使用连接进行查询插入

  DB::connection('mysql')->table('users')->insert(['URL'=>$name]);
如果使用默认连接,也不需要连接方法

进口

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

首先,您需要编辑
.env
文件

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=registration
DB_USERNAME=root
DB_PASSWORD=secretPassword
插入数据会像这样工作

首先需要导入数据库类
使用light\Support\Facades\DB

然后执行insert语句

DB::insert('insert into book(URL)value(?),[$name])

表中可能有不止一列

DB::insert('insert into books(URL,另一列)值(?,),[$name,'anotherValue'])


Source

我现在说不出我有多感激。还有一件事,我的整个项目都在xampp中,我在那里制作和运行PHP文件,现在我希望当用户点击一个按钮时,我的laravel项目打开,我希望这能用localhost/在xampp上运行。。无需使用artisan服务器的路径。我怎么能做到这一点?我在这篇文章中尝试了几件事,它打开了项目,但当我单击“上载”按钮时,它不起作用,因为本地主机/上载的路径不存在这超出了我的知识范围。避免100%执行
DB::insert(“…”)
,使用模型或至少
DB::table('books')->..
。。。尽量避免使用
DB
类。。。