- vừa được xem lúc

Multiple File Images Upload Trong Laravel

0 0 8

Người đăng: Gấu con

Theo Viblo Asia

Hi mọi người.
Bài viết này mình sẽ chia sẻ cách upload mutiple file images trong laravel 8. Ở ví dụ mình demo sẽ bao gồm các chức năng như create, edit file images.
Các bạn hãy theo dõi các bước bên dưới để hiểu hơn nhé.

Màn hình Preview create.


Màn hình Preview edit.

Step1: Tạo Migration và Model

Mình sẽ tạo migration và model cho table files như bên dưới nhé

php artisan make:migration create_files_table

Migration:

<?php use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; class CreateFilesTable extends Migration
{ /** * Run the migrations. * * @return void */ public function up() { Schema::create('files', function (Blueprint $table) { $table->id(); $table->string('filenames'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('files'); }
} 
php artisan migrate

Tiếp theo mình sẽ tạo model file bằng command bên dưới.

php artisan make:model File

app/Models/File.php

<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; class File extends Model
{ use HasFactory; protected $fillable = [ 'filenames' ]; /** * Set the user's first name. * * @param string $value * @return void */ public function setFilenamesAttribute($value) { $this->attributes['filenames'] = json_encode($value); }
} 

Step2: Create Routes

Mình sẽ tạo 4 routes như bên dưới để demo chức năng upload, edit ảnh nhé.

<?php use Illuminate\Support\Facades\Route; /*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/ Route::get('create/', '_@.com');
Route::post('create/', '_@.com');
Route::get('edit/{id}', '_@.com')->name('file.edit');
Route::post('edit/', '_@.com');

Step3: Create Controller

app/Http/Controllers/FileController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request;
use App\Models\File;
use Illuminate\Support\Facades\File as File2; class FileController extends Controller
{ /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function create() { $files = File::all(); return view('create', ['files' => $files]); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'filenames' => 'required', 'filenames.*' => 'required' ]); $files = []; if($request->hasfile('filenames')) { foreach($request->file('filenames') as $file) { $name = time().rand(1,100).'.'.$file->extension(); $file->move(public_path('files'), $name); $files[] = $name; } } $file= new File(); $file->filenames = $files; $file->save(); return back()->with('success', 'Data Your files has been successfully added'); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function edit($id) { $files = File::find($id); return view('create', ['list_images' => $files->filenames, 'id' => $id]); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function update(Request $request) { $input = $request->all(); $files = []; $files_remove = []; if($request->hasfile('filenames')) { foreach($request->file('filenames') as $file) { $name = time().rand(1,100).'.'.$file->extension(); $file->move(public_path('files'), $name); $files[] = $name; } } if (isset($input['images_uploaded'])) { $files_remove = array_diff(json_decode($input['images_uploaded_origin']), $input['images_uploaded']); $files = array_merge($input['images_uploaded'], $files); } else { $files_remove = json_decode($input['images_uploaded_origin']); } $file = File::find($input['id']); $file->filenames = $files; if($file->update()) { foreach ($files_remove as $file_name) { File2::delete(public_path("files/".$file_name)); } } return back()->with('success', 'Data Your files has been successfully updated'); }
} 

Tham khảo 3 cách lưu ảnh vào thư mục nhé.
Store File in Storage Folder

$file->storeAs('files', $imageName); // storage/app/files/file.png

Store File in Public Folder

$file->move(public_path('files'), $imageName); // public/files/file.png

Store File in S3

$file->storeAs('files', $imageName, 's3');

Step4: Create Blade File

Bước này mình sẽ tạo file create.blade.php trong resources nhé.
resources/views/create.blade.php

<html lang="en">
<head> <title>Multiple File Images Upload</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <style> .list-images { width: 50%; margin-top: 20px; display: inline-block; } .hidden { display: none; } .box-image { width: 100px; height: 108px; position: relative; float: left; margin-left: 5px; } .box-image img { width: 100px; height: 100px; } .wrap-btn-delete { position: absolute; top: -8px; right: 0; height: 2px; font-size: 20px; font-weight: bold; color: red; } .btn-delete-image { cursor: pointer; } .table { width: 15%; } </style>
</head>
<body> <div class="container lst"> @if (count($errors) > 0)
<div class="alert alert-danger"> <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 class="alert alert-success"> {{ session('success') }}
</div> @endif
@if (isset($list_images)) <form method="post" action="{{url('edit')}}" enctype="multipart/form-data">
@else <form method="post" action="{{url('create')}}" enctype="multipart/form-data"> @endif @csrf <div class="input-group hdtuto control-group lst increment" > <div class="list-input-hidden-upload"> <input type="file" name="filenames[]" id="file_upload" class="myfrm form-control hidden"> </div> <div class="input-group-btn"> <button class="btn btn-success btn-add-image" type="button"><i class="fldemo glyphicon glyphicon-plus"></i>+Add image</button> </div> </div> <div class="list-images"> @if (isset($list_images) && !empty($list_images)) @foreach (json_decode($list_images) as $key => $img) <div class="box-image"> <input type="hidden" name="images_uploaded[]" value="{{ $img }}" id="img-{{ $key }}"> <img src="{{ asset('files/'.$img) }}" class="picture-box"> <div class="wrap-btn-delete"><span data-id="img-{{ $key }}" class="btn-delete-image">x</span></div> </div> @endforeach <input type="hidden" name="images_uploaded_origin" value="{{ $list_images }}"> <input type="hidden" name="id" value="{{ $id }}"> @endif </div> <div class="button-submit"> <button type="submit" class="btn btn-success" style="margin-top:10px"> @if (isset($list_images)) Update @else Submit @endif </button> </div> </form> @if (isset($files) && $files->count() > 0) <table class="table table-bordered"> <thead> <tr> <th scope="col">#</th> <th scope="col">Edit</th> </tr> </thead> <tbody> @foreach ($files as $elm) <tr> <th scope="row">{{ $elm->id }}</th> <td><a href="{{ route('file.edit',['id' => $elm->id]) }}"><button type="button" class="btn btn-info">Edit</button></a></td> </tr> @endforeach </tbody> </table> @endif
</div> <script type="text/javascript"> $(document).ready(function() { $(".btn-add-image").click(function(){ $('#file_upload').trigger('click'); }); $('.list-input-hidden-upload').on('change', '#file_upload', function(event){ let today = new Date(); let time = today.getTime(); let image = event.target.files[0]; let file_name = event.target.files[0].name; let box_image = $('<div class="box-image"></div>'); box_image.append('<img src="' + URL.createObjectURL(image) + '" class="picture-box">'); box_image.append('<div class="wrap-btn-delete"><span data-id='+time+' class="btn-delete-image">x</span></div>'); $(".list-images").append(box_image); $(this).removeAttr('id'); $(this).attr( 'id', time); let input_type_file = '<input type="file" name="filenames[]" id="file_upload" class="myfrm form-control hidden">'; $('.list-input-hidden-upload').append(input_type_file); }); $(".list-images").on('click', '.btn-delete-image', function(){ let id = $(this).data('id'); $('#'+id).remove(); $(this).parents('.box-image').remove(); }); });
</script> </body>
</html>

Cuối cùng là chạy và kiểm tra code đã hoạt động hay chưa .
Mọi người có góp ý hoặc chưa hiểu thì hãy comment bên dưới nhé.

Hy vọng bài viết này có ích cho các bạn!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Tìm hiểu về Resource Controller trong Laravel

Giới thiệu. Trong laravel, việc sử dụng các route post, get, group để gọi đến 1 action của Controller đã là quá quen đối với các bạn sử dụng framework này.

0 0 335

- vừa được xem lúc

Phân quyền đơn giản với package Laravel permission

Như các bạn đã biết, phân quyền trong một ứng dụng là một phần không thể thiếu trong việc phát triển phần mềm, dù đó là ứng dụng web hay là mobile. Vậy nên, hôm nay mình sẽ giới thiệu một package có thể giúp các bạn phân quyền nhanh và đơn giản trong một website được viết bằng PHP với framework là L

0 0 420

- vừa được xem lúc

Sử dụng Swagger để xây dựng API documentation

Giới thiệu về Swagger. RESTful API là một tiêu chuẩn dùng trong việc thiết kế API cho các ứng dụng web (thiết kế Web services) để tiện cho việc quản lý các resource.

0 0 1k

- vừa được xem lúc

Ví dụ CRUD với Laravel và Vuejs.

1. Cài đặt Laravel. composer create-project --prefer-dist laravel/laravel vuelaravelcrud. .

0 0 140

- vừa được xem lúc

Một số tips khi dùng laravel (Part 1)

1. Show database query in raw SQL format. DB::enableQueryLog(); // Bật tính năng query logging. DB::table('users')->get(); // Chạy truy vấn bạn muốn ghi log.

0 0 69

- vừa được xem lúc

Inertiajs - Xây dựng Single Page App không cần API

Tiêu đề là mình lấy từ trang chủ của https://inertiajs.com/ chứ không phải mình tự nghĩ ra đâu nhé :v. Lâu lâu rồi chưa động tới Laravel (dự án cuối cùng mình code là ở ver 5.8), thế nên một ngày đẹp trời lượn vào đọc docs ver 8.

0 0 227