Related
Using Laravel 5.4
I'm trying to submit a form with a file to upload with ajax.
My jquery code:
$("#uploadRoastsForm").submit(function() {
var url = $(this).attr('action');
var fd = new FormData();
var files = $('#roastFile')[0].files;
if(files.length > 0)
fd.append('file', files[0]);
fd.append('file', files[0]);
fd.append('roast_date', $('#roastDate').val());
$.ajax({
url: url,
type: 'POST',
data: fd,
contentType: false,
processData: false,
success: function (data) {...
My form:
<form action="/warehouse/upload" method="post" enctype="multipart/form-data" class="form" id="uploadRoastsForm">
{{ csrf_field() }}
<div class="form-group" style="width:150px">
<label>Roasting Date</label>
<div class="input-group">
<input name="roast_date" type="text" value="{{ old('roast_date') }}" class="form-control datepick" id="roastDate">
<div class="input-group-addon">
<span class="fa fa-calendar"></span>
</div>
</div>
</div>
<div class="form-group">
<label>Upload Roast Files</label>
<input type="file" name="file" id="roastFile">
</div>
I am getting this in firebug:
XHRPOSThttp://highland.local/warehouse/upload
[HTTP/1.1 419 unknown status 250ms]
message ""
exception
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php"
line 204
trace [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
0 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", line: 176, function: "prepareException", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php"
line 176
function "prepareException"
class "Illuminate\\Foundation\\Exceptions\\Handler"
type "->"
1 Object { file: "C:\\wamp64\\www\\highland.local\\app\\Exceptions\\Handler.php", line: 77, function: "render", … }
file "C:\\wamp64\\www\\highland.local\\app\\Exceptions\\Handler.php"
line 77
function "render"
class "Illuminate\\Foundation\\Exceptions\\Handler"
type "->"
2 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 83, function: "render", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 83
function "render"
class "App\\Exceptions\\Handler"
type "->"
3 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 55, function: "handleException", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 55
function "handleException"
class "Illuminate\\Routing\\Pipeline"
type "->"
4 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php", line: 49, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php"
line 49
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
5 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\View\\Middleware\\ShareErrorsFromSession"
type "->"
6 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
7 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php", line: 63, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php"
line 63
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
8 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Session\\Middleware\\StartSession"
type "->"
9 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
10 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php", line: 37, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php"
line 37
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
11 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse"
type "->"
12 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
13 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php", line: 66, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php"
line 66
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
14 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Cookie\\Middleware\\EncryptCookies"
type "->"
15 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
16 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 104, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 104
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
17 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 684, function: "then", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 684
function "then"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
18 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 659, function: "runRouteWithinStack", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 659
function "runRouteWithinStack"
class "Illuminate\\Routing\\Router"
type "->"
19 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 625, function: "runRoute", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 625
function "runRoute"
class "Illuminate\\Routing\\Router"
type "->"
20 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 614, function: "dispatchToRoute", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 614
function "dispatchToRoute"
class "Illuminate\\Routing\\Router"
type "->"
21 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 176, function: "dispatch", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 176
function "dispatch"
class "Illuminate\\Routing\\Router"
type "->"
22 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 30, function: "Illuminate\\Foundation\\Http\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 30
function "Illuminate\\Foundation\\Http\\{closure}"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
23 Object { file: "C:\\wamp64\\www\\highland.local\\app\\Http\\Middleware\\ForceSsl.php", line: 29, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\app\\Http\\Middleware\\ForceSsl.php"
line 29
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
24 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "App\\Http\\Middleware\\ForceSsl"
type "->"
25 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
26 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", line: 31, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php"
line 31
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
27 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest"
type "->"
28 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
29 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", line: 31, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php"
line 31
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
30 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest"
type "->"
31 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
32 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php", line: 27, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php"
line 27
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
33 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize"
type "->"
34 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
35 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 104, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 104
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
36 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 151, function: "then", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 151
function "then"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
37 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 116, function: "sendRequestThroughRouter", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 116
function "sendRequestThroughRouter"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
38 Object { file: "C:\\wamp64\\www\\highland.local\\public\\index.php", line: 53, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\public\\index.php"
line 53
function "handle"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
I also tried to have an input as a submit button on the form and when I click it I call the exact same ajax, but then I got the error: MethodNotAllowed.
Something breaks when I use: contentType: false, processData: false. But if I don't use them I can't send the file to the controller.
Can anyone help please?
How do you upload file with ajax in Laravel?
At first you didn't posted what is the method in your route/web.php for your ajax request.
If it is MethodNotAllowed related problem then probably your ajax request and route method is not same.
If you are trying to update something then use PUT method in your route like below
Route::put('/warehouse/upload', "WarehouseController#upload');
And if you are trying to insert data into database table then use POST method:
Route::post('/warehouse/upload', "WarehouseController#upload');
As you are using POST method from your jQuery AJAX function, then just use post method in your route.
I think it should solve your query.
I have such a problem I have a directory in laravel called temp I do not know why not tmp I even gave permission to the whole directory laravel chmod -R 777 laravel and continue the same and I know what to do, but how to use the library mpdf it gets the message
Laravel Temporary files directory "/var/www/html/laravel/../temp/" is not writable
(1/1) MpdfException
Temporary files directory "/var/www/html/laravel/../temp/" is not writable
in Cache.php line 17
at Cache->__construct('/var/www/html/laravel/../temp/')
in Mpdf.php line 983
at Mpdf->__construct(array('tempDir' => '/var/www/html/laravel/../temp/', 'mirrorMargins' => 0, 'forcePortraitMargins' => false, 'displayDefaultOrientation' => false, 'printers_info' => false, 'bleedMargin' => 5, 'crossMarkMargin' => 5, 'cropMarkMargin' => 8, 'cropMarkLength' => 18, 'nonPrintMargin' => 8, 'defaultPagebreakType' => 'cloneall', 'margBuffer' => 2, 'pagenumPrefix' => '', 'pagenumSuffix' => '', 'nbpgPrefix' => '', 'nbpgSuffix' => '', 'defaultPageNumStyle' => '1', 'aliasNbPg' => '{nb}', 'aliasNbPgGp' => '{nbpg}', 'maxTTFFilesize' => 2000, 'percentSubset' => 30, 'useAdobeCJK' => false, 'repackageTTF' => false, 'allow_charset_conversion' => true, 'biDirectional' => false, 'autoScriptToLang' => false, 'baseScript' => 1, 'autoVietnamese' => true, 'autoArabic' => true, 'autoLangToFont' => false, 'useSubstitutions' => false, 'falseBoldWeight' => 5, 'allow_output_buffering' => false, 'enableImports' => false, 'collapseBlockMargins' => true, 'dpi' => 96, 'allow_html_optional_endtags' => true, 'ignore_invalid_utf8' => false, 'text_input_as_HTML' => false, 'incrementFPR1' => 10, 'incrementFPR2' => 20, 'incrementFPR3' => 30, 'incrementFPR4' => 50, 'restrictColorSpace' => 0, 'PDFX' => false, 'PDFXauto' => false, 'PDFA' => false, 'PDFAauto' => false, 'ICCProfile' => '', 'spotColors' => array(), 'spotColorIDs' => array(), 'debug' => false, 'debugfonts' => false, 'showImageErrors' => false, 'table_error_report' => false, 'table_error_report_param' => '', 'title2annots' => false, 'annotSize' => 0.5, 'annotMargin' => null, 'annotOpacity' => 0.5, 'anchor2Bookmark' => 0, 'bookmarkStyles' => array(), 'h2bookmarks' => array(), 'h2toc' => array(), 'indexUseSubentries' => true, 'CSSselectMedia' => 'print', 'forcePortraitHeaders' => false, 'defaultheaderfontsize' => 8, 'defaultheaderfontstyle' => 'BI', 'defaultheaderline' => 1, 'defaultfooterfontsize' => 8, 'defaultfooterfontstyle' => 'BI', 'defaultfooterline' => 1, 'header_line_spacing' => 0.25, 'footer_line_spacing' => 0.25, 'setAutoTopMargin' => false, 'setAutoBottomMargin' => false, 'autoMarginPadding' => 2, 'simpleTables' => false, 'packTableData' => false, 'ignore_table_percents' => false, 'ignore_table_widths' => false, 'keep_table_proportions' => true, 'shrink_tables_to_fit' => 1.3999999999999999, 'tableMinSizePriority' => false, 'use_kwt' => false, 'iterationCounter' => false, 'splitTableBorderWidth' => 0, 'decimal_align' => array('DP' => '.', 'DC' => ',', 'DM' => '·', 'DA' => '٫', 'DD' => '-'), 'interpolateImages' => false, 'img_dpi' => 96, 'useKerning' => false, 'justifyB4br' => false, 'tabSpaces' => 8, 'jSWord' => 0.40000000000000002, 'jSmaxChar' => 2, 'jSmaxCharLast' => 1, 'jSmaxWordLast' => 2, 'useFixedNormalLineHeight' => false, 'useFixedTextBaseline' => false, 'normalLineheight' => 1.3300000000000001, 'adjustFontDescLineheight' => 1.1399999999999999, 'smCapsScale' => 0.75, 'smCapsStretch' => 110, 'useDictionaryLBR' => true, 'useTibetanLBR' => true, 'allowCJKorphans' => true, 'allowCJKoverflow' => false, 'CJKforceend' => false, 'keepColumns' => false, 'max_colH_correction' => 1.1499999999999999, 'ColGap' => 5, 'list_auto_mode' => 'browser', 'list_indent_default' => '40px', 'list_indent_default_mpdf' => '0em', 'list_indent_first_level' => 0, 'list_number_suffix' => '.', 'list_marker_offset' => '5.5pt', 'list_symbol_size' => '3.6pt', 'SHYlanguages' => array('en', 'de', 'es', 'fi', 'fr', 'it', 'nl', 'pl', 'ru', 'sv'), 'SHYlang' => 'en', 'SHYleftmin' => 2, 'SHYrightmin' => 2, 'SHYcharmin' => 2, 'SHYcharmax' => 10, 'useActiveForms' => false, 'watermarkImgBehind' => false, 'showWatermarkText' => 0, 'showWatermarkImage' => 0, 'watermarkText' => '', 'watermarkAngle' => 45, 'watermarkImage' => '', 'watermark_font' => '', 'watermarkTextAlpha' => 0.20000000000000001, 'watermarkImageAlpha' => 0.20000000000000001, 'watermarkImgAlphaBlend' => 'Normal', 'autoPadding' => false, 'svgAutoFont' => false, 'svgClasses' => false, 'defaultCSS' => array('BODY' => array('FONT-FAMILY' => 'serif', 'FONT-SIZE' => '11pt', 'TEXT-INDENT' => '0pt', 'LINE-HEIGHT' => 'normal', 'MARGIN-COLLAPSE' => 'collapse', 'HYPHENS' => 'manual', 'FONT-KERNING' => 'auto'), 'P' => array('MARGIN' => '1.12em 0'), 'H1' => array('FONT-SIZE' => '2em', 'FONT-WEIGHT' => 'bold', 'MARGIN' => '0.67em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'H2' => array('FONT-SIZE' => '1.5em', 'FONT-WEIGHT' => 'bold', 'MARGIN' => '0.75em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'H3' => array('FONT-SIZE' => '1.17em', 'FONT-WEIGHT' => 'bold', 'MARGIN' => '0.83em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'H4' => array('FONT-WEIGHT' => 'bold', 'MARGIN' => '1.12em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'H5' => array('FONT-SIZE' => '0.83em', 'FONT-WEIGHT' => 'bold', 'MARGIN' => '1.5em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'H6' => array('FONT-SIZE' => '0.75em', 'FONT-WEIGHT' => 'bold', 'MARGIN' => '1.67em 0', 'PAGE-BREAK-AFTER' => 'avoid'), 'HR' => array('COLOR' => '#888888', 'TEXT-ALIGN' => 'center', 'WIDTH' => '100%', 'HEIGHT' => '0.2mm', 'MARGIN-TOP' => '0.83em', 'MARGIN-BOTTOM' => '0.83em'), 'PRE' => array('MARGIN' => '0.83em 0', 'FONT-FAMILY' => 'monospace'), 'S' => array('TEXT-DECORATION' => 'line-through'), 'STRIKE' => array('TEXT-DECORATION' => 'line-through'), 'DEL' => array('TEXT-DECORATION' => 'line-through'), 'SUB' => array('VERTICAL-ALIGN' => 'sub', 'FONT-SIZE' => '55%'), 'SUP' => array('VERTICAL-ALIGN' => 'super', 'FONT-SIZE' => '55%'), 'U' => array('TEXT-DECORATION' => 'underline'), 'INS' => array('TEXT-DECORATION' => 'underline'), 'B' => array('FONT-WEIGHT' => 'bold'), 'STRONG' => array('FONT-WEIGHT' => 'bold'), 'I' => array('FONT-STYLE' => 'italic'), 'CITE' => array('FONT-STYLE' => 'italic'), 'Q' => array('FONT-STYLE' => 'italic'), 'EM' => array('FONT-STYLE' => 'italic'), 'VAR' => array('FONT-STYLE' => 'italic'), 'SAMP' => array('FONT-FAMILY' => 'monospace'), 'CODE' => array('FONT-FAMILY' => 'monospace'), 'KBD' => array('FONT-FAMILY' => 'monospace'), 'TT' => array('FONT-FAMILY' => 'monospace'), 'SMALL' => array('FONT-SIZE' => '83%'), 'BIG' => array('FONT-SIZE' => '117%'), 'ACRONYM' => array('FONT-SIZE' => '77%', 'FONT-WEIGHT' => 'bold'), 'ADDRESS' => array('FONT-STYLE' => 'italic'), 'BLOCKQUOTE' => array('MARGIN-LEFT' => '40px', 'MARGIN-RIGHT' => '40px', 'MARGIN-TOP' => '1.12em', 'MARGIN-BOTTOM' => '1.12em'), 'A' => array('COLOR' => '#0000FF', 'TEXT-DECORATION' => 'underline'), 'UL' => array('PADDING' => '0 auto', 'MARGIN-TOP' => '0.83em', 'MARGIN-BOTTOM' => '0.83em'), 'OL' => array('PADDING' => '0 auto', 'MARGIN-TOP' => '0.83em', 'MARGIN-BOTTOM' => '0.83em'), 'DL' => array('MARGIN' => '1.67em 0'), 'DT' => array(), 'DD' => array('PADDING-LEFT' => '40px'), 'TABLE' => array('MARGIN' => '0', 'BORDER-COLLAPSE' => 'separate', 'BORDER-SPACING' => '2px', 'EMPTY-CELLS' => 'show', 'LINE-HEIGHT' => '1.2', 'VERTICAL-ALIGN' => 'middle', 'HYPHENS' => 'manual', 'FONT-KERNING' => 'auto'), 'THEAD' => array(), 'TFOOT' => array(), 'TH' => array('FONT-WEIGHT' => 'bold', 'TEXT-ALIGN' => 'center', 'PADDING-LEFT' => '0.1em', 'PADDING-RIGHT' => '0.1em', 'PADDING-TOP' => '0.1em', 'PADDING-BOTTOM' => '0.1em'), 'TD' => array('PADDING-LEFT' => '0.1em', 'PADDING-RIGHT' => '0.1em', 'PADDING-TOP' => '0.1em', 'PADDING-BOTTOM' => '0.1em'), 'CAPTION' => array('TEXT-ALIGN' => 'center'), 'IMG' => array('MARGIN' => '0', 'VERTICAL-ALIGN' => 'baseline', 'IMAGE-RENDERING' => 'auto'), 'INPUT' => array('FONT-FAMILY' => 'sans-serif', 'VERTICAL-ALIGN' => 'middle', 'FONT-SIZE' => '0.9em'), 'SELECT' => array('FONT-FAMILY' => 'sans-serif', 'FONT-SIZE' => '0.9em', 'VERTICAL-ALIGN' => 'middle'), 'TEXTAREA' => array('FONT-FAMILY' => 'monospace', 'FONT-SIZE' => '0.9em', 'VERTICAL-ALIGN' => 'text-bottom'), 'MARK' => array('BACKGROUND-COLOR' => 'yellow')), 'defaultCssFile' => '/var/www/html/laravel/vendor/mpdf/mpdf/src/Config/../../data/mpdf.css', 'customProperties' => array(), 'languageToFont' => object(LanguageToFont), 'scriptToLanguage' => object(ScriptToLanguage), 'pdf_version' => '1.4', 'fontDir' => array('/var/www/html/laravel/vendor/mpdf/mpdf/src/Config/../../ttfonts'), 'allowAnnotationFiles' => false, 'hyphenationDictionaryFile' => '/var/www/html/laravel/vendor/mpdf/mpdf/src/Config/../../data/patterns/dictionary.txt', 'default_lineheight_correction' => 1.2, 'fontsizes' => array('XX-SMALL' => 0.69999999999999996, 'X-SMALL' => 0.77000000000000002, 'SMALL' => 0.85999999999999999, 'MEDIUM' => 1, 'LARGE' => 1.2, 'X-LARGE' => 1.5, 'XX-LARGE' => 2), 'pregRTLchars' => '\\x{0590}-\\x{06FF}\\x{0700}-\\x{085F}\\x{FB00}-\\x{FDFD}\\x{FE70}-\\x{FEFF}', 'pregCJKchars' => '\\x{1100}-\\x{11FF}\\x{2E80}-\\x{A4CF}\\x{A800}-\\x{D7AF}\\x{F900}-\\x{FAFF}\\x{FE30}-\\x{FE6F}\\x{FF00}-\\x{FFEF}\\x{20000}-\\x{2FA1F}', 'CJKleading' => '$\\(\\*\\[\\{\\x{00a3}\\x{00a5}\\x{00ab}\\x{00b7}\\x{2018}\\x{201c}\\x{2035}\\x{3005}\\x{3007}\\x{3008}\\x{300a}\\x{300c}\\x{300e}\\x{3010}\\x{3014}\\x{3016}\\x{3018}\\x{301d}\\x{fe34}\\x{fe35}\\x{fe37}\\x{fe39}\\x{fe3b}\\x{fe3d}\\x{fe3f}\\x{fe41}\\x{fe43}\\x{fe57}\\x{fe59}\\x{fe5b}\\x{fe5d}\\x{ff04}\\x{ff08}\\x{ff0e}\\x{ff3b}\\x{ff5b}\\x{ff5f}\\x{ff62}\\x{ffe1}\\x{ffe5}\\x{ffe6}', 'CJKfollowing' => '!%\\),\\.:,>\\?\\]\\}\\x{00a2}\\x{00a8}\\x{00b0}\\x{00b7}\\x{00bb}\\x{02c7}\\x{02c9}\\x{2010}\\x{2013}-\\x{2016}\\x{2019}\\x{201d}-\\x{201f}\\x{2020}-\\x{2022}\\x{2025}-\\x{2027}\\x{2030}\\x{2032}\\x{2033}\\x{203a}\\x{203c}\\x{2047}-\\x{2049}\\x{2103}\\x{2236}\\x{2574}\\x{3001}-\\x{3003}\\x{3005}\\x{3006}\\x{3009}\\x{300b}\\x{300d}\\x{300f}\\x{3011}\\x{3015}\\x{3017}\\x{3019}\\x{301c}\\x{301e}\\x{301f}\\x{303b}\\x{3041}\\x{3043}\\x{3045}\\x{3047}\\x{3049}\\x{3063}\\x{3083}\\x{3085}\\x{3087}\\x{308e}\\x{3095}\\x{3096}\\x{309b}-\\x{309e}\\x{30a0}\\x{30a1}\\x{30a3}\\x{30a5}\\x{30a7}\\x{30a9}\\x{30c3}\\x{30e3}\\x{30e5}\\x{30e7}\\x{30ee}\\x{30f5}\\x{30f6}\\x{30fb}-\\x{30fd}\\x{30fe}\\x{31f0}-\\x{31ff}\\x{fe30}\\x{fe31}-\\x{fe34}\\x{fe36}\\x{fe38}\\x{fe3a}\\x{fe3c}\\x{fe3e}\\x{fe40}\\x{fe42}\\x{fe44}\\x{fe4f}\\x{fe50}-\\x{fe58}\\x{fe5a}\\x{fe5c}-\\x{fe5e}\\x{ff01}\\x{ff02}\\x{ff05}\\x{ff07}\\x{ff09}\\x{ff0c}\\x{ff0e}\\x{ff1a}\\x{ff1b}\\x{ff1f}\\x{ff3d}\\x{ff40}\\x{ff5c}-\\x{ff5e}\\x{ff60}\\x{ff61}\\x{ff63}-\\x{ff65}\\x{ff9e}\\x{ff9f}\\x{ffe0}', 'CJKoverflow' => '\\.,\\x{ff61}\\x{ff64}\\x{3001}\\x{3002}\\x{fe50}-\\x{fe52}\\x{ff0c}\\x{ff0e}', 'pregCURSchars' => '\\x{0590}-\\x{083E}\\x{0900}-\\x{0DFF}\\x{FB00}-\\x{FDFD}\\x{FE70}-\\x{FEFF}', 'allowedCSStags' => 'DIV|P|H1|H2|H3|H4|H5|H6|FORM|IMG|A|BODY|TABLE|HR|THEAD|TFOOT|TBODY|TH|TR|TD|UL|OL|LI|PRE|BLOCKQUOTE|ADDRESS|DL|DT|DD|ARTICLE|ASIDE|FIGURE|FIGCAPTION|FOOTER|HEADER|HGROUP|NAV|SECTION|MAIN|MARK|DETAILS|SUMMARY|METER|PROGRESS|TIME|SPAN|TT|I|B|BIG|SMALL|EM|STRONG|DFN|CODE|SAMP|KBD|VAR|CITE|ABBR|ACRONYM|STRIKE|S|U|DEL|INS|Q|FONT|SELECT|INPUT|TEXTAREA|CAPTION|FIELDSET|LEGEND|TEXTCIRCLE|DOTTAB|BDO|BDI', 'outerblocktags' => array('DIV', 'FORM', 'CENTER', 'DL', 'FIELDSET', 'ARTICLE', 'ASIDE', 'FIGURE', 'FIGCAPTION', 'FOOTER', 'HEADER', 'HGROUP', 'MAIN', 'NAV', 'SECTION', 'DETAILS', 'SUMMARY', 'UL', 'OL', 'LI'), 'innerblocktags' => array('P', 'BLOCKQUOTE', 'ADDRESS', 'PRE', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'DT', 'DD', 'CAPTION'), 'curlFollowLocation' => false, 'curlAllowUnsafeSslRequests' => false, 'curlTimeout' => 5))
in Controller_strona.php line 60
at Controller_strona->generate_pdf()
at call_user_func_array(array(object(Controller_strona), 'generate_pdf'), array())
in Controller.php line 55
at Controller->callAction('generate_pdf', array())
in ControllerDispatcher.php line 44
at ControllerDispatcher->dispatch(object(Route), object(Controller_strona), 'generate_pdf')
in Route.php line 203
at Route->runController()
in Route.php line 160
at Route->run()
in Router.php line 572
at Router->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in SubstituteBindings.php line 41
at SubstituteBindings->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in VerifyCsrfToken.php line 65
at VerifyCsrfToken->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 102
at Pipeline->then(object(Closure))
in Router.php line 574
at Router->runRouteWithinStack(object(Route), object(Request))
in Router.php line 533
at Router->dispatchToRoute(object(Request))
in Router.php line 511
at Router->dispatch(object(Request))
in Kernel.php line 176
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
in Pipeline.php line 30
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in TransformsRequest.php line 30
at TransformsRequest->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in ValidatePostSize.php line 27
at ValidatePostSize->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
in Pipeline.php line 148
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request))
in Pipeline.php line 102
at Pipeline->then(object(Closure))
in Kernel.php line 151
at Kernel->sendRequestThroughRouter(object(Request))
in Kernel.php line 116
at Kernel->handle(object(Request))
in index.php line 53
Maybe ownership of the file could be causing a problem. Your file's group ownership might be set as 'root' so you have to change it to 'www-data'
this is what i usually do...
chown -R root:www-data {your_file}
Hope if fixes the problem
I am importing customers from old website to magento. While importing via CSV file, I get the following error:
Invalid password length in rows: 36, 42, 54, 142, 160, 219, 298, 341, 352, 376, 428, 430, 460, 481, 510, 511, 535, 561, 565, 570, 588, 601, 661, 672, 701, 727, 774, 779, 847, 865, 876, 892, 912, 924, 932, 943, 951, 952, 953, 972, 988, 1017, 1022, 1030, 1059, 1068, 1091, 1147, 1150, 1153, 1155, 1156, 1157, 1158, 1161, 1162, 1163, 1165, 1167, 1175, 1177, 1178, 1179, 1180, 1181, 1183, 1187, 1188, 1189, 1190, 1191, 1197, 1198, 1199, 1200, 1201, 1203, 1207, 1210, 1211, 1212, 1219, 1224, 1225, 1254, 1268, 1279, 1290, 1301, 1302, 1304, 1322, 1332, 1342, 1367, 1378...
How can I remove this validation in magento programmatically or is there any easier way to solve this?
You need to remove the validation both programmatically as on frontend, otherwise a user has a short pass but can't log in due to javascript validation.
http://www.magentocommerce.com/boards/viewthread/18197/
UPDATE: Possible solution at the end, but certainly not performant or ideal.
I created the following method that gives me close to what I want.
def multi_permutations(collection)
case collection.length
when 1
return collection.shift[1]
when 0
raise "You must pass in a multidimensional collection."
end
a = collection.shift[1]
b = multi_permutations(collection)
return_value = []
a.each do |a_value|
b.each do |b_value|
return_value << [a_value] + [b_value]
end
end
return return_value
end
When I pass in a hash with nested arrays that looks like this...
my_collection["item_9"] = [152]
my_collection["item_2"] = [139, 143, 145]
my_collection["item_13"] = [138, 142, 150]
my_collection["item_72"] = [137, 149, 151, 154]
my_collection["item_125"] = [140, 141]
my_collection["item_10"] = [144, 146, 147, 148, 153]
I want it to create an array of hashes with all permutations that looks like this...
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }]
.
.
.
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }]
What this function ends up doing is close but I lose the relationships.
[152, [139, [138, [137, [140, 144]]]]]
[152, [139, [138, [137, [140, 146]]]]]
[152, [139, [138, [137, [140, 147]]]]]
[152, [139, [138, [137, [140, 148]]]]]
[152, [139, [138, [137, [140, 153]]]]]
.
.
.
[152, [145, [150, [154, [141, 144]]]]]
[152, [145, [150, [154, [141, 146]]]]]
[152, [145, [150, [154, [141, 147]]]]]
[152, [145, [150, [154, [141, 148]]]]]
[152, [145, [150, [154, [141, 153]]]]]
The relationships are very important to me. The reason is, I plan to hydrate an object where the attrs are the keys in the hash. I'm sure this can be done in a better way, and I'm open to suggestions.
So one possible solution I've come up with is to create a keys array then flatten the permutations and zip them together in a hash.
results = []
permutations = multi_permutations(possibilities)
permutations.each do |permutation|
results << Hash[keys.zip permutation.flatten!]
end
This ends up giving me...
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>146}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>147}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>148}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>153}
I couldn't get your solution to run because you did not provide how to call it, so I couldn't compare the efficiency.
What do you think about this solution? (the single argument has to be a Hash of Arrays, just like your my_collection)
my_collection = {}
my_collection["item_9"] = [152]
my_collection["item_2"] = [139, 143, 145]
my_collection["item_13"] = [138, 142, 150]
my_collection["item_72"] = [137, 149, 151, 154]
my_collection["item_125"] = [140, 141]
my_collection["item_10"] = [144, 146, 147, 148, 153]
def permutations!(input)
input.each do |key, possibilities|
possibilities.map!{|p| {key => p} }
end
digits = input.keys.map!{|key| input[key] }
digits.shift.product(*digits)
end
results = permutations!(my_collection)
Please notice that this method modifies the input object because of the map! usage.
I have an array of arrays of hashes.
items =
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }]
.
.
.
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }]
What I'd like to do is change it to be an array of hashes...
items =
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>146}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>147}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>148}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>153}
I believe I can do this using...
items.map! { |item| item.reduce({}, :merge) }
However, it's not very performant. At least it's not performant enough when you have 140million records. Is there a better way to do this?
perhaps a bit longer but it works much quicker:
require 'benchmark'
items = [
[{ item_9: 152 }, { item_2: 139 }, { item_13: 138 }, { item_72: 137 }, { item_125: 140 }, { item_10: 146 }],
[{ item_9: 152 }, { item_2: 139 }, { item_13: 138 }, { item_72: 137 }, { item_125: 140 }, { item_10: 147 }],
[{ item_9: 152 }, { item_2: 139 }, { item_13: 138 }, { item_72: 137 }, { item_125: 140 }, { item_10: 148 }],
[{ item_9: 152 }, { item_2: 139 }, { item_13: 138 }, { item_72: 137 }, { item_125: 140 }, { item_10: 153 }],
[{ item_9: 152 }, { item_2: 145 }, { item_13: 150 }, { item_72: 154 }, { item_125: 141 }, { item_10: 144 }],
[{ item_9: 152 }, { item_2: 145 }, { item_13: 150 }, { item_72: 154 }, { item_125: 141 }, { item_10: 146 }],
[{ item_9: 152 }, { item_2: 145 }, { item_13: 150 }, { item_72: 154 }, { item_125: 141 }, { item_10: 147 }],
]
n = 100_000
Benchmark.bm do |b|
b.report do
n.times do |i|
items.map { |item| item.reduce({}, :merge) }
end
end
b.report do
n.times do |i|
# the winer
items.map { |item| item.reduce({}, :update) }
end
end
b.report do
n.times do |i|
items.map { |i| i.inject({}) { |f,c| f.update c } }
end
end
end
as #tokland suggests, item.reduce({}, :update) is even faster:
user system total real
6.300000 0.080000 6.380000 ( 6.386180)
1.840000 0.020000 1.860000 ( 1.860073)
2.220000 0.020000 2.240000 ( 2.237294)
thanks #tokland
Since performance is an issue, it might be time for for loops yields as well as noticing interesting facts about your data, if any. For example, your data seem to have many repetitive items. Is that a rule or just a coincidence?
If you’re sure you have a two-level array (no other arrays inside the pairs) and exactly two items in each pair, then it’s faster and shorter to use this:
array = [['A', 'a'], ['B', 'b'], ['C', 'c']]
hash = Hash[*array.flatten]
For more than two-level deep arrays this will give the wrong result or even an error (for some inputs).
array = [['A', 'a'], ['B', 'b'], ['C', ['a', 'b', 'c']]]
hash = Hash[*array.flatten]
# => {"A"=>"a", "B"=>"b", "C"=>"a", "b"=>"c"}
But if you’re running Ruby 1.8.7 or greater you can pass an argument to Array#flatten and have it flatten only one level deep:
# on Ruby 1.8.7+
hash = Hash[*array.flatten(1)]
# => {"A"=>"a", "B"=>"b", "C"=>["a", "b", "c"]}