0

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?

SA Ira
  • 63
  • 11

3 Answers3

1

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");
Milad pegah
  • 321
  • 1
  • 8
0

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;
John Lobo
  • 14,355
  • 2
  • 10
  • 20
-1

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

noah1400
  • 1,282
  • 1
  • 4
  • 15
  • i just can't tell how much thankful I am right now. – SA Ira Jun 06 '21 at 17:22
  • There is just one more thing, my whole project is in xampp where I am making and running PHP files, now I want that when user click on a button then my laravel project opens, and I want this to work on xampp using localhost/.. path without having to use artisan server. How can I do this? – SA Ira Jun 06 '21 at 17:25
  • I tried few things https://stackoverflow.com/questions/28788285/how-to-run-laravel-without-artisan from this post, it opens the project but when I click upload button then it does not work that the path localhost/uploads is not present – SA Ira Jun 06 '21 at 17:29
  • This is beyond my knowledge – noah1400 Jun 06 '21 at 18:13
  • Ok great thanks for your help, it means a lot. I was stuck for days – SA Ira Jun 06 '21 at 18:15
  • 1
    Avoid 100% doing `DB::insert('.....')`, use models or at least `DB::table('books')->....`... Avoid using `DB` class as much as possible... – matiaslauriti Jun 06 '21 at 19:23
  • @matiaslauriti why? it is recommended in the laravel documentation. – noah1400 Jun 06 '21 at 19:30
  • Please, share the link where it explicitly recommends using `DB::insert()`... – matiaslauriti Jun 06 '21 at 19:32
  • https://laravel.com/docs/8.x/database#running-queries – noah1400 Jun 06 '21 at 19:48
  • @noah1400 that is nowhere recommending you to use it... You have to learn a lot my friend... You have to read the documentation better as you clearly have no idea why Laravel is so useful related to databases... Read the introduction to it's ORM, [Eloquent](https://laravel.com/docs/8.x/eloquent#introduction)... – matiaslauriti Jun 06 '21 at 23:44
  • @matiaslauriti I still don't see the problem using `DB::insert()`. – noah1400 Jun 07 '21 at 05:44
  • @noah1400 The idea of using Laravel is not only based on it simple MVC... but also on the ease of use about Models/Entites = data... you use Models not a raw query... you really want to write raw SQL instead of, for example: `User::create(['name' => 'John']);` ? – matiaslauriti Jun 07 '21 at 06:14
  • @matiaslauriti you are right it's easier and prettier to use models . I just didn't know you can create models on an existing and already configured database – noah1400 Jun 07 '21 at 07:24
  • @noah1400 Awesome, read the documentation I shared, you will see a lot of great stuff !!! And you will stop using `DB::` ! Also, the advantage is for `relations`, you do not need to do `DB::innerJoin` and stuff, eloquent is beautiful. – matiaslauriti Jun 07 '21 at 15:50