1

I have never used laravel and have no knowledge about it. I used it for uploading files to google drive. Now I have a file 'web.php' which upload the files.

<?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");

the file which has HTML code is like this:


<!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 that from the web.php file, i get $name and display the value in my HTML code div. How can I possibly do this?

SA Ira
  • 63
  • 11

1 Answers1

0

To me it looks like you want to use a variable that does not exist yet. The $name variable is defined after the form is submitted. If it is possible to define the variable before the form is submitted the following applies

You can't pass a Variable from a post route. You need to create another route to show your form and use the post route to process the data. For example

web.php

<!-- web.php -->
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
Route::get('/', function () {
    return view('welcome');
});

//Route to show the form

Route::get('/upload',function (){
    $value = "ValueThatShouldBeDisplayed";
    return view('showForm', ['value'=>$value]);
})->name('upload.show');//route name

//Route to process the data

Route::post('/upload', function (Request $request) {
    $name=$request->file("thing")->getClientOriginalName();
    Storage::disk("google")->putFileAs("",$request->file("thing"),$name);

    //redirecting to home or something
    return redirect('/');

})->name("upload.store");//route name

Your Html must be stored in \ressources\views\showForm.blade.php

showForm.blade.php

<!-- showForm.blade.php -->

<!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>
                    <!-- store route as action -->
                    <form action="{{ route('upload.store') }}" method="post" enctype="multipart/form-data">
                    @csrf
                    
                    <!-- value Part -->
                    {{ $value }}

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

If you want to change content as soon as for example you select a file you should take a look at Vue components.

noah1400
  • 1,282
  • 1
  • 4
  • 15