How to upload image in laravel 8

How to upload Image in laravel 8


In this tutorail, you will be learning about image upload in laravel. Where we create a form and upload image in folder and save its name into database in laravel 8.

We will be using Bootstrap 5 to design the user interface. Let's begin for upload image in laravel 8.

Step 1: Create a migration table named students with following command:

$ php artisan make:migration create_students_table
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('image');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

Step 2: Create a Eloquent Model named Student with following command:

$ php artisan make:model Student
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;

    protected $table = 'students';

    protected $fillable = [
        'name',
        'image',
    ];
}

Step 3: Create a controller named StudentController with the following commad:

$ php artisan make:controller StudentController

After successful execution of above command, open the StudentController on the following path as: app/Http/Controllers/StudentController.php and paste the below code in it.

Note: create a folder in your public directory as: uploads/students

<?php

namespace App\Http\Controllers\API;

use App\Models\Student;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class StudentController extends Controller
{
    public function store(Request $request)
    {
        $student = new Student;
        $student->name = $request->input('name');

        if($request->hasfile('image'))
        {
            $file = $request->file('image');
            $extenstion = $file->getClientOriginalExtension();
            $filename = time().'.'.$extenstion;
            $file->move('uploads/students/', $filename);
            $student->image = $filename;
        }

        $student->save();
        return redirect()->back()->with('message','Student Image Upload Successfully');
    }
}

Step 4: Create a blade file named studentCreate.blade.php in the following path: resources/views/student.blade.php to create a form to upload image.

@extends('layouts.app')

@section('content')

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6">

            <div class="card">
                <div class="card-header">
                    <h4>Student Form</h4>
                </div>
                <div class="card-body">

                    <form action="{{ url('save-student') }}" method="POST" enctype="multipart/form-data">
                        @csrf

                        <div class="mb-3">
                            <label for="">Name</label>
                            <input type="text" name="name" required class="form-control">
                        </div>
                        <div class="mb-3">
                            <label for="">Upload Image</label>
                            <input type="file" name="image" required class="course form-control">
                        </div>
                        <div class="mb-3">
                            <button type="submit" class="btn btn-primary">Save</button>
                        </div>

                    </form>

                </div>
            </div>

        </div>
    </div>
</div>

@endsection

Step 5: Create a route in the following path: routes/web.php and paste the below code.

Route::post('students', [App\Http\Controllers\StudentController::class, 'store']);

Step 6: serve the artisan and start uploading the image.

$ php artisan serve

That's it. You will find your image in your uploads/students folder.


Thank you.