我正在创建一个图像编辑订单系统,用户可以通过DropZone& Laravel。我想在成功将图像上传后的图像信息保存到数据库&向用户发送电子邮件通知。然后使用会话消息重定向到另一个页面。每次将数据保存到数据库时,我的代码都会发送超过1封电子邮件。但是,当所有图像数据成功保存到数据库时,我需要发送电子邮件。这是我的代码:
$order = Order::find($request->input('order_id'));
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $filename,
'file_size' => $file->getClientSize(),
'file_path' => $path . '/' . $filename,
]);
// When all file details saved to database send Single Notification mail
$data = array(
'fullname' => $order->user->fullname,
'email' => $order->user->email,
'order_number' => $order->order_number
);
Mail::send('emails.order-submit', $data, function($message) use ($data) {
$message->from('test@gmail.com');
$message->to($data['email']);
$message->subject('Order Submit');
});
这是我的完整控制器代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Model\Order;
use Session;
use Mail;
use Illuminate\Support\Facades\Auth;
class Images1Controller extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function doImageUpload(Request $request) {
//get the file from the post request
$file = $request->file('file');
//set the file name
$filename = $file->getClientOriginalName();
//move the file to correct location
$order_number = $request->input('oid');
$username = Auth::user()->username;
$path = 'orders/'. $username .'/' . $order_number;
if(!file_exists($path)){
mkdir($path, 0777, true);
}
$file->move($path, $filename);
//save the multiple/single file details to database
$order = Order::find($request->input('order_id'));
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $filename,
'file_size' => $file->getClientSize(),
'file_mime' => $file->getClientMimeType(),
'file_path' => $path . '/' . $filename,
]);
Session::put('success','Your order has been submitted successfully.');
$data = array(
'fullname' => $order->user->fullname,
'email' => $order->user->email,
'order_number' => $order->order_number
);
Mail::send('emails.order-submit', $data, function($message) use ($data) {
$message->from('test@gmail.com');
$message->to($data['email']);
$message->subject('Order Submit');
});
}
}
DropZone配置代码:
Dropzone.options.uploadImages = {
autoProcessQueue: false,
maxFilesize: 2048,
uploadMultiple: false,
parallelUploads: 100,
acceptedFiles: 'image/*',
addRemoveLinks: true,
dictRemoveFile: 'Remove',
init: function() {
var submitButton = document.querySelector("#submit-images")
addImages = this; // closure
submitButton.addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
addImages.processQueue(); // Tell Dropzone to process all queued files.
});
this.on("addedfile", function(file) {
// Show submit button here and/or inform user to click it.
});
this.on("success", function() {
if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
window.location.href = 'http://localhost:8000/user/orders/manage/';
}
});
}
};
答案 0 :(得分:1)
试试这个:
$order = Order::find($request->input('order_id'));
foreach( $request-order_id as $key=>$val){
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $filename,
'file_size' => $file->getClientSize(),
'file_mime' => $file->getClientMimeType(),
'file_path' => $path . '/' . $filename,
]);
}
// When all file details saved to database send Single Notification mail
$data = array(
'fullname' => $order->user->fullname,
'email' => $order->user->email,
'order_number' => $order->order_number
);
Mail::send('emails.order-submit', $data, function($message) use ($data) {
$message->from('test@gmail.com');
$message->to($data['email']);
$message->subject('Order Submit');
});
答案 1 :(得分:0)
FORM输入应该是这样的
<input type="file" name="images[]" multiple />
doImageUpload方法的代码是
if ($request->hasFile('images')) {
$files = $request->file('images');
foreach($files as $file) {
//set the file name
$filename = $file->getClientOriginalName();
//move the file to correct location
$order_number = $request->input('oid');
$username = Auth::user()->username;
$path = 'orders/'. $username .'/' . $order_number;
if(!file_exists($path)){
mkdir($path, 0777, true);
}
$file->move($path, $filename);
//save the multiple/single file details to database
$order = Order::find($request->input('order_id'));
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $filename,
'file_size' => $file->getClientSize(),
'file_mime' => $file->getClientMimeType(),
'file_path' => $path . '/' . $filename,
]);
}
Session::put('success','Your order has been submitted successfully.');
$data = array(
'fullname' => $order->user->fullname,
'email' => $order->user->email,
'order_number' => $order->order_number
);
Mail::send('emails.order-submit', $data, function($message) use ($data) {
$message->from('test@gmail.com');
$message->to($data['email']);
$message->subject('Order Submit');
});
}
答案 2 :(得分:0)
解决方案是DropZone发送单个请求而不是多个请求
关于DropZone配置的 uploadMultiple: true,
。现在需要将控制器代码更新为:
foreach($file as $key => $value) {
//save the multiple/single file details to database
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $file[$key]->getClientOriginalName(),
'file_size' => $file[$key]->getClientSize(),
'file_mime' => $file[$key]->getClientMimeType(),
'file_path' => $path,
]);
//Move Files to destination path
$file[$key]-> move($path, $file[$key]->getClientOriginalName());
}
以下是更新的完整上传方法:
public function doImageUpload(Request $request) {
//get the file from the post request
$file = $request->file('file');
//move the file to correct location
$order = Order::find($request->input('order_id'));
$order_number = $order->order_number;
$now = Carbon\Carbon::now();
$year = date('Y', strtotime($now));
$month = date('M', strtotime($now));
$datetime = date('Ymd', strtotime($now));
$username = Auth::user()->username;
// public/orders/year/month/date/username/order-number
$path = 'orders/' . $year . '/' . $month . '/' . $datetime .'/' . $username . '/' . $order_number;
foreach($file as $key => $value) {
//save the multiple/single file details to database
$order->images()->create([
'order_id' => $request->input('order_id'),
'file_name' => $file[$key]->getClientOriginalName(),
'file_size' => $file[$key]->getClientSize(),
'file_mime' => $file[$key]->getClientMimeType(),
'file_path' => $path,
]);
//Move Files to destination path
$file[$key]-> move($path, $file[$key]->getClientOriginalName());
}
// Send Email Notification to User & Admin
$time = Carbon\Carbon::now();
Mail::to($order->user)->later($time,new PlaceOrder($order));
Mail::to('test@gmail.com')->later($time,new NewOrder($order));
Session::put('success','Your Order Submitted Success.');
return $order;
}
此处更新了Full DropZone配置:
Dropzone.options.uploadImages = {
autoProcessQueue: false,
maxFilesize: 2048,
uploadMultiple: true,
parallelUploads: 100,
acceptedFiles: 'image/*',
addRemoveLinks: true,
dictRemoveFile: 'Remove',
init: function() {
var submitButton = document.querySelector("#submit-images")
addImages = this; // closure
submitButton.addEventListener("click", function(e) {
e.preventDefault();
e.stopPropagation();
addImages.processQueue(); // Tell Dropzone to process all queued files.
});
this.on("addedfile", function(file) {
// Show submit button here and/or inform user to click it.
});
this.on("success", function() {
if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
window.location.href = 'http://localhost:8000/user/orders/manage/';
}
});
}
};