Tuesday, December 6, 2022
Laravel 6MysqlPhp

How to upload multiple images in laravel 6?

upload multiple images in laravel 6 with ajax

Hello to all, welcome to 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('') @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>
                <div class="alert alert-success">
                    <p> {{session("message")}} </p>
                <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>
                        <button type="submit" class="btn btn-primary">Publish</button>

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,];
        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.


Thank you

the authortherichpost
Hello to all. Welcome to Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 8, Angular 9, Angular 10, Angular 11, Angular12, Angular 13, Angular 14, Angular 15, 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.