Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel中插入带有文件上载循环的数组数据库_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 在laravel中插入带有文件上载循环的数组数据库

Php 在laravel中插入带有文件上载循环的数组数据库,php,laravel,laravel-5,Php,Laravel,Laravel 5,视图代码 @foreach($questions as $question) <input type="hidden" name="question[{{$question->id}}]" value="{{$question->id}}"> <textarea name="answers[{{$question->id}}]"></textarea> <i

视图代码

@foreach($questions as $question)
<input type="hidden" name="question[{{$question->id}}]" value="{{$question->id}}">
<textarea name="answers[{{$question->id}}]"></textarea>
<input type="file" name="answerfile[{{$question->id}}]">
@endforeach
答案插入正确,但文件不是按行上传的。如果上载循环的所有文件,则它可以工作,但插入一行,如果为空,则任何一个文件都将无法正常工作。 请帮助我逐行插入数据和文件。
请随意分享另一个想法。

我认为您需要尝试一下

    foreach ($request->get('question') as $key => $answer) {
        $answers[] = [
            'quiz_id' => $quiz->id,
            'user_id' => $user_record->id,
            'written_question_id' => $answer->question,
            'answers' => $request->get('answers')[$key],
            'answerfile' => $request->file('answerfile')[$key],
        ];
    }
    $this->validate($request, [

        'answerfile' => 'required',
        'answerfile.*' => 'mimes:doc,pdf,docx,zip'
    ]);

    if ($request->hasfile('answerfile')[$key]) {
        $file = $request->file('answerfile')[$key];
        $name = $file->getClientOriginalName();
        $file->move(public_path() . '/uploads/written/', $name);
        $data[$answer] = $name;
    }else{
        $data[$answer]="no_img.png";
    }

我希望它对您有用。

我已经解决了我的问题。特别感谢@yagna pathak

foreach ( $request->get('question') as $key => $answer) {
    $name = '';
        if (isset($request->file('answerfile')[$key])) {
            $file = $request->file('answerfile')[$key];
            $name = time()."-".rand(9,999999)."-".$file->getClientOriginalName();
            $file->move(public_path() . '/uploads/written/', $name);
        }
    $answers[] = [
        'quiz_id' => $quiz->id,
        'user_id' => $user_record->id,
        'written_question_id' => $answer,
        'answers' => $request->get('answers')[$key],
        'answerfile' => $name
    ];
}
我想看看这个:


请在插入之前先显示dd($answers)。数组:1[▼ 0=>数组:9[▼ “测验id”=>2“用户id”=>12“书面问题id”=>4“答案”=>answerfile=>UploadedFile{779▼ } ] ]#779▼ -测试:false-原始名称:“27863 Amphan Relief--2.docx”-mimeType:“application/vnd.openxmlformats of cedocument.wordprocessingml.document”-大小:13004-错误:0#hashName:null路径:“E:\xampp\tmp”文件名:“phpF130.tmp”基本名称:“phpF130.tmp”路径名:“E:\xampp\tmp\phpF130.tmp”扩展名:“tmp”realPath:false writable:false readable:false Executive:false file:false dir:false link:false我还需要放置文件,我可以以这些名称保存在数据库中,但我如何处理这些文件以上载我的服务器?你有什么解决方案吗?你只能在数据库中保存文件名,在一个数据库中保存文件特殊文件夹。若文件不能上传到任何一行,那个么就把虚拟文件放在你们的特殊文件夹中,把虚拟文件名放在数据库中。你们能给我一个解决方案吗?我已经编辑了我的答案,在回答中你们可以在你们的文件夹(public_path()。
foreach ( $request->get('question') as $key => $answer) {
    $name = '';
        if (isset($request->file('answerfile')[$key])) {
            $file = $request->file('answerfile')[$key];
            $name = time()."-".rand(9,999999)."-".$file->getClientOriginalName();
            $file->move(public_path() . '/uploads/written/', $name);
        }
    $answers[] = [
        'quiz_id' => $quiz->id,
        'user_id' => $user_record->id,
        'written_question_id' => $answer,
        'answers' => $request->get('answers')[$key],
        'answerfile' => $name
    ];
}