Home Laravel 6 How to upload multiple images in laravel 6?

How to upload multiple images in laravel 6?

by therichpost
0 comments
upload multiple images in laravel 6 with ajax

Hello to all, welcome to therichpost.com. In this post, I will tell you, How to upload multiple images in laravel 6?

Post Working:

In this post, I am uploading multiple images in laravel 6.

Here is the working code snippet and please follow carefully:

1. Here is the code , you need to add into your Blade template file:

Here is the code for upload image(HTML CODE) and you can add this code any blade template file:

@extends('layouts.app') @section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Add Media Images : </div>
                @if(session()->has("message"))
                <div class="alert alert-success">
                    <p> {{session("message")}} </p>
                </div>
                @endif
                <div class="card-body">
                    <form action="{{ url('addmedia') }}" enctype="multipart/form-data" method="post">
                        {{ csrf_field() }}
                        <div class="form-group">
                            <label for="image"><strong>Add Media:</strong></label>
                            <input type="file" class="form-control" id="image" placeholder="Post Image" name="image[]" required multiple>
                        </div>
                        <button type="submit" class="btn btn-primary">Publish</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

2. Here is code for route/web.php file:

This code will connect form data to controller file:

Route::post('/addmedia', 'HomeController@createmedia');

3. Here is the controller file code app\Http\Controllers\HomeController.php file:

This code will save images into folder and database and I have created images table into my database :

public function createmedia(Request $request)
    {
        if (!$request->hasFile('image')) {
      return Redirect::to('add-post')->with("message","Missing image!");
        }
        if ($request->file('image')) {
      foreach ($request->file('image') as $photo) {
            $file      = $photo;
            $filename  = $file->getClientOriginalName();
            $extension = $file->getClientOriginalExtension();
            $picture     = date('His').'-'.$filename;
            $pictures[]   = $picture;
      
      //Save files in below folder path, that will make in public folder
            $file->move(public_path('pages/'), $picture);
      $postArray = ['image' => $picture,];
            DB::table('images')->insert($postArray);
            }
        
        return Redirect::to('add-post')->with("message","Media added successfully.");
    }
        
    }

This is it and if you have any kind of query then please do comment below.

Jassa,

Thank you

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.