Categories

Sunday, November 24, 2024
#919814419350 therichposts@gmail.com
Laravel 6MysqlPhp

How to upload multiple images in laravel 6?

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 19, MedusaJs, Next.js, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

Leave a Reply

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