Laravel 7 Multiple File Upload Tutorial

Laravel 7 Multiple File Upload Tutorial

In this post we will give you information about Laravel 7 Multiple File Upload Tutorial. Hear we will give you detail about Laravel 7 Multiple File Upload TutorialAnd how to use it also give you demo for it if it is necessary.

In this example, you will learn upload multiple files in laravel 7. you’ll learn upload multiple files in php laravel 7. this example will help you multiple file upload laravel 7. i would like to share with you laravel 7 multiple file upload example.

We will create simple multiple file upload in laravel 7. So you basically use this code on your laravel 7 application.

We are going from starch so, we will upload multiple file and store on server then after we will store database too. so in this example we will create “files” table using laravel migration and write code for route, controller and view step by step.

So here you have to just follow bellow step to create Laravel 7 multiple File Upload with Validation Example.

Step 1: Download Laravel 7

In the first step, we will download a new simple copy source code of Laravel App project by typing the some following command.

composer create-project --prefer-dist laravel/laravel blog

Step 2: Add Migration and Model

Here, we need create database migration for files table and also we will create model for files table.

php artisan make:migration create_files_table

Migration:

<?php


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;


class CreateFormsTable extends Migration

{

public function up()

{

Schema::create('files', function (Blueprint $table) {

$table->increments('id');

$table->string('filenames');

$table->timestamps();

});

}


public function down()

{

Schema::dropIfExists('files');

}

}

php artisan migrate

now we will create File model by using following command:

php artisan make:model File

Also see:Laravel 7 CRUD Example | Laravel 7 Tutorial For Beginners

Step 3: Create Routes

In third step, we will create routes for multiple file upload. so create two route with GET and POST route example.

routes/web.php

Route::get('file','FileController@create');

Route::post('file','FileController@store');

Step 4: Create Controller

Now we require to add new FileController controller for manage route So let’s create FileController with create and store method. Make sure you need to create “files” folder in your public directory.

app/Http/Controllers/FileController.php

<?php


namespace AppHttpControllers;


use IlluminateHttpRequest;


class FileController extends Controller

{

/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function create()

{

return view('create');

}


/**

* Show the application dashboard.

*

* @return IlluminateHttpResponse

*/

public function store(Request $request)

{


$this->validate($request, [

'filenames' => 'required',

'filenames.*' => 'mimes:doc,pdf,docx,zip'

]);


if($request->hasfile('filenames'))

{

foreach($request->file('filenames') as $file)

{

$name = time().'.'.$file->extension();

$file->move(public_path().'/files/', $name);

$data[] = $name;

}

}


$file= new File();

$file->filenames=json_encode($data);

$file->save();


return back()->with('success', 'Data Your files has been successfully added');

}

}

Step 5: Create Blade File

in this step we need to create create.blade.php file in resources folder. So let’s create file:

resources/views/create.blade.php

Also see:Laravel 7 Multiple Image Upload Example

<html lang="en">

<head>

<title>Laravel 7 Multiple File Upload Example</title>

<script src="jquery/1.9.1/jquery.js"></script>

<link rel="stylesheet" href="3.3.6/css/bootstrap.min.css">

</head>

<body>


<div >


@if (count($errors) > 0)

<div >

<strong>Sorry!</strong> There were more problems with your HTML input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


@if(session('success'))

<div >

{{ session('success') }}

</div>

@endif


<h3 >Laravel 7 Multiple File Upload</h3>

<form method="post" action="{{url('file')}}" enctype="multipart/form-data">

{{csrf_field()}}


<div >

<input type="file" name="filenames[]" >

<div >

<button type="button"><i ></i>Add</button>

</div>

</div>

<div >

<div style="margin-top:10px">

<input type="file" name="filenames[]" >

<div >

<button type="button"><i ></i> Remove</button>

</div>

</div>

</div>


<button type="submit" style="margin-top:10px">Submit</button>


</form>

</div>


<script type="text/javascript">

$(document).ready(function() {

$(".btn-success").click(function(){

var lsthmtl = $(".clone").html();

$(".increment").after(lsthmtl);

});

$("body").on("click",".btn-danger",function(){

$(this).parents(".hdtuto control-group lst").remove();

});

});

</script>


</body>

</html>

Now you can simple run and check it.

I hope it can help you…

Hope this code and post will helped you for implement Laravel 7 Multiple File Upload Tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

  +  4  =  7

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US