我有一个允许多个条目进入数据库的表单。每个行都包含一个文件上载字段。
字段创建如下:
{{ Form::select('revision[]', ['0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6'], '0', ['class' => 'form-control artwork-revision']); }}
{{ Form::text('product[]', false, ['class' => 'form-control artwork-product', 'placeholder' => 'Please enter the product name']) }}
{{ Form::file('file[]', ['class' => 'artwork-file']) }}
我的控制器有一个foreach循环,可以将每一行输入数据库,但是当我运行它时,我收到以下错误消息:preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
代码适用于单个文件上传(即没有foreach循环,只插入一行,没有方括号创建的字段)
如何解决此错误并将信息输入数据库?
这是我的控制器中的foreach循环和对象的var_dump
。
控制器foreach循环:
$files = Input::file('file');
foreach($files as $file) {
// it's a new artwork row
$artwork = new Artwork;
// get the vars
$artwork->job_id = Input::get('job_id');
$artwork->revision = Input::get('revision');
$artwork->product = Input::get('product');
// it's pending
$artwork->status = 'P';
// sort the filename...
$filename = $file->getClientOriginalName();
$file = $file->move(base_path() . '/public/artwork/' . Input::get('job_id'), $filename);
// ...and put it in the $artwork object
$artwork->filename = 'artwork/' . $artwork->job_id . '/' . $filename;
// save it
$artwork->save();
}
var_dump($ artwork)输出 - 请注意,此处只显示一个图像文件名而不是两个:
object(Artwork)#243 (21) {
["dates":protected]=>
array(1) {
[0]=>
string(10) "deleted_at"
}
["fillable":protected]=>
array(6) {
[0]=>
string(6) "job_id"
[1]=>
string(8) "filename"
[2]=>
string(6) "status"
[3]=>
string(8) "revision"
[4]=>
string(7) "product"
[5]=>
string(6) "reason"
}
["table":protected]=>
string(8) "artworks"
["connection":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(5) {
["job_id"]=>
string(1) "5"
["revision"]=>
array(2) {
[0]=>
string(1) "0"
[1]=>
string(1) "0"
}
["product"]=>
array(2) {
[0]=>
string(15) "Twist USB Drive"
[1]=>
string(19) "Eco Twist USB Drive"
}
["status"]=>
string(1) "P"
["filename"]=>
string(24) "artwork/5/12345-test.jpg"
}
["original":protected]=>
array(0) {
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["appends":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["touches":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["morphClass":protected]=>
NULL
["exists"]=>
bool(false)
["forceDeleting":protected]=>
bool(false)
}
答案 0 :(得分:1)
这是我的新店'解决问题的控制器:
$artwork = new Artwork;
// standard bits
$artwork->job_id = Input::get('job_id');
$artwork->status = 'P';
// variables
$artwork->revision = Input::get('revision');
$artwork->product = Input::get('product');
$artwork->file = Input::file('file');
// count how many pieces of artwork are being uploaded (this could be any field)
$count = count($artwork->revision);
/* multi-file upload */
$i = 0;
// process each piece
foreach($artwork as $a) {
while($count > $i) {
// it's a new piece of artwork
$a = new Artwork;
// standard bits to object
$a->job_id = $artwork->job_id;
$a->status = $artwork->status;
// revision and product name to object
$a->revision = $artwork->revision[$i];
$a->product = $artwork->product[$i];
// get the file and move it
$file = $artwork->file[$i];
$filename = $file->getClientOriginalName();
$movefile = $file->move(base_path() . '/public/artwork/' . $artwork->job_id, $filename);
// filename to object
$a->filename = $filename;
// save the object to db
$a->save();
// add 1 to the count
$i++;
}
}
return Redirect::route('jobs.index');
答案 1 :(得分:0)
我认为你甚至不能选择多个文件。将文件字段替换为多个属性设置为true的文件字段,这将允许一次选择多个文件并返回文件数组。
{{ Form::file('file[]', ['class' => 'artwork-file','multiple' => true]) }}