Search code examples
phpsqllaravelstorelaravel-8

How to upload files on laravel and storing it in a directory


So I am having problems storing my picture of the user in the specified directory. The image is already in the database but when I call the data it does not show anything. How do I store it in the public folder and the database here is my lines of code:

RegisterController.php

<?php

namespace App\Http\Controllers;

use Hash;
use App\Models\User;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
  public function signin()
  {
    return view('register', [
      'register' => true
    ]);
  }

  public function register(Request $request)
  {
    // Laravel Eloquent
    $user = User::where('email', $request->get('email'))->first();
    if ($user) {
      // if user / email exists
      return view('register', [
        'register' => false
      ]);
    } else {
      // create user
      User::create([
        'name' => $request->get('name'),
        'email' => $request->get('email'),
        'password' => Hash::make($request->get('password')),
        'user_type' => 'admin',
        'bdate' => $request->get('bdate'),
        'contact_no' => '',
        'profile_image' => $request->file('profile_image')->store('http://localhost:8000/storage/images/'),
        //...
      ]);
  
      return redirect(route('login.login', ['registration' => 1]));
    }
  }
}

and here is the registration form

register.blade.php

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Register</title>

    <!-- Custom fonts for this template-->
    <link href="{{asset('dashboards/vendor/fontawesome-free/css/all.min.css')}}" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="{{asset('dashboards/css/sb-admin-2.min.css')}}" rel="stylesheet">

    <style>
        .new-bg-register-image {
             background: url("{{asset('dashboards/img/Benedicto.jpg')}}");
             background-position: center;
             background-size: cover;
        }
    </style>

</head>

<body class="bg-gradient-primary">

    <div class="container">

        <div class="card o-hidden border-0 shadow-lg my-5">
            <div class="card-body p-0">
                <!-- Nested Row within Card Body -->
                <div class="row">
                    <div class="col-lg-5 d-none d-lg-block new-bg-register-image"></div>
                    <div class="col-lg-7">
                        <div class="p-5">
                            <div class="text-center">
                                <h1 class="h4 text-gray-900 mb-4">Create an Account!</h1>

                                @if (!$register)
                                    <div class="alert alert-danger" role="alert">
                                        Email already exist.
                                    </div>
                                @endif
                            </div>
                            <form class="user" method="POST" action="{{ route('register.register') }}">
                                @csrf
                                <div class="form-group">
                                    <label for="name" class="form-label">Name</label>
                                        <input type="text" class="form-control form-control-user" id="name"
                                            placeholder="Name" name="name" required>
                                    </div>
                                    <div class="form-group">
                                    <label for="email" class="form-label">Email address</label>
                                        <input type="email" class="form-control form-control-user" id="email"
                                            placeholder="Email Address" name="email" required>
                                    </div>
                                    <div class="form-group">
                                    
                                        <label for="password" class="form-label">Password</label>
                                            <input type="password" class="form-control form-control-user"
                                                id="password" placeholder="Password" name="password" required>
                                    </div>
                                    <div class="form-group">
                                  <label for="profile_image" class="form-label">Upload profile picture</label>
                                <input type="file" class="form-control-file form-control-user" id="profile_image" name="profile_image" required>
                            </div>
                                    <div class="form-group">
                                    
                                        <label for="bdate" class="form-label">Birthdate</label>
                                            <input type="date" class="form-control form-control-user"
                                                id="bdate" placeholder="Birthdate" name="bdate" required>
                                    </div>

                                    <button type="submit" class="btn btn-primary btn-user btn-block">Register</button>
                                </div>
                                <hr>
                            </form>
                            <hr>
                            <div class="text-center">
                                <a class="small" href="{{route('login.login')}}">Already have an account? Login!</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="{{asset('dashboards/vendor/jquery/jquery.min.js')}}"></script>
    <script src="{{asset('dashboards/vendor/bootstrap/js/bootstrap.bundle.min.js')}}"></script>

    <!-- Core plugin JavaScript-->
    <script src="{{asset('dashboards/vendor/jquery-easing/jquery.easing.min.js')}}"></script>

    <!-- Custom scripts for all pages-->
    <script src="{{asset('dashboards/js/sb-admin-2.min.js')}}"></script>

</body>

</html>

Solution

  • you can use Storage:: class to do that

    'profile_image' => $request->file('profile_image') ? Storage::disk('public')->put('images',$request->profile_image) : null
    

    then it will store path inside database to get full url of that file you can again use Storage::url()

    like

    $url = \Storage::disk('public')->url($user->profile_image)
    

    you need to add enctype to your form tag like this

    <form class="user" method="POST" action="{{ route('register.register') }}" enctype="multipart/form-data">
    

    ref link https://laravel.com/docs/8.x/filesystem