I have the web.php file
<?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");
?>
and HTML code like
<!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>
I want the data that is received in $name variable in the Route::post
to be saved in the database. like if I have MySQL database name "registration" having table "books" and I want to save this value in column "URL" How can I do this?
First move the file and after that save the records:
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");
First for checking database table exist or not
Schema::connection('mysql')->hasTable('books')
this will return true if exist or else return false
for inserting into database
DB::table('books')->insert(['URL'=>$name])
Final code will be
$tableExist=Schema::connection('mysql')->hasTable('books');
if($tableExist){
DB::table('books')->insert(['URL'=>$name]);
}
Also you can use connection for query insert also
DB::connection('mysql')->table('users')->insert(['URL'=>$name]);
Also connection method not required if you are using default connection.
For imports
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
First you need to edit your .env
file.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=registration
DB_USERNAME=root
DB_PASSWORD=secretPassword
Inserting your data would work like that
First you need to import the Database class use Illuminate\Support\Facades\DB;
Then execute the insert statement
DB::insert('insert into books (URL) values (?)', [$name]);
You probably have more then one column inside your table
DB::insert('insert into books (URL, AnotherColumn) values (?,?)', [$name,'anotherValue']);
Source laravel-docs
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.