Laravel

How to upload Image in laravel

Upload Image in laravel

We will learn about upload image in laravel. We will create a form and upload image or any file in folder and save its name into database in laravel 8.

We will be using any design user interface here we use Bootstrap 5 to design the user interface. Let’s begin for upload image in laravel 8.

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

$ php artisan make:migration create_users_table
<?php

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

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

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

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

$ php artisan make:model Student
<?php

namespace App\Models;

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

class User extends Model
{
    use HasFactory;

    protected $table = 'users';

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

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

$ php artisan make:controller UserController

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

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

<?php

namespace App\Http\Controllers\API;

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

class UserController extends Controller
{
    public function store(Request $request)
    {
        $users = new User;
        $users->name = $request->input('name');

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

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

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

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

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

                    <form action="{{ url('users') }}" 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>

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

Route::post('users', [App\Http\Controllers\UserController::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/user folder.

Visited 8 times, 1 visit(s) today

Leave a Reply

Your email address will not be published. Required fields are marked *