Laravel后端没有保存到数据库

时间:2017-08-01 23:12:08

标签: javascript angularjs laravel http

我使用laravel作为后端API REST上传文件。当我在前端上传文件时,它会在响应中发送200 OK状态代码,但不会将其保存到数据库中。我觉得有些奇怪的是,在响应中数据变空,文件数组出现在响应的config部分。 200 ok response

这是处理这种情况的控制器

 public function makeFile(Request $request)
    {
        $fileArray = [];
    if (isset($request->files) && is_array($request->files)) {
        foreach ($request->files as $key => $fileEntity) {
            $file= new File();
            $file->file=$fileEntity['file'];
            $file->shipment_id=$fileEntity['shipment_id'];
            $file->user_id=$fileEntity['user_id'];
            $file->date=date('Y,m,d,G,i,s');
            $file->fileName=$fileEntity['fileName'];
            $file->fileType=$fileEntity['fileType'];
            $file->status=$fileEntity['status'];
            $file->save();

            $fileArray[] = $file;
        }
    }
    return response()->json($fileArray);
    }

这是我的表格

<uib-tab index="1" heading="Files">
                        <div class="row">
                        <label>Add File</label>
                            <ul class="shipment__files">
                                <li class="col-xs-12 col-md-6" ng-repeat="file in shipment.shipmentObject.files">
                                    <a href="#"><img ng-src="{{ file.iconURL }}" width="60"></a>
                                    <div class="filename"><a href="#">{{ file.fileName }}</a></div>
                                    <div class="date">{{ file.date }}</div>
                                    <div class="uploader">{{ file.user_id }}</div>
                                </li>
                            </ul>
                        </div>
                        <!-- <form>
                        <input type="file" name="" ng-model="shipment.shipmentObject.file">
                        <button class="btn btn-primary" ng-click="shipment.makeFile(shipmentObject.file)" >Upload File</button>

                        <div>aa {{ newFile }}</div>
                        </form> -->
                        <div>
                          <input data-my-Directive type="file" name="file" ng-model="shipment.shipmentObject.file">
                          <p>
                            post request with this data:
                          </p>
                          <pre ng-repeat="(key, value) in fileLog">
                            {{key}} {{ value }}
                          </pre>
                        </div>

                    </uib-tab>

1 个答案:

答案 0 :(得分:0)

因为您的条件语句不匹配,所以您收到空的回复,因此您只是返回您创建的空podTemplate( label: 'mypod', volumes: [ persistentVolumeClaim(claimName: 'nfs-maven', mountPath: '/mnt/', readOnly: false)], containers: [ containerTemplate( name: 'golang', image: 'golang', ttyEnabled: true, command: 'cat', envVars: [containerEnvVar(key: 'FOO', value: 'BAR')] ) ] ) { node('mypod') { stage('test env') { container('golang') { stage('build') { sh 'echo $FOO' sh 'sleep 3600' } } } } }

$fileArray

检查是否有文件的正确方法是:

if (isset($request->files) && is_array($request->files))

您需要了解的有关此内容的信息,请参阅此处的官方文档:

https://laravel.com/docs/5.4/requests#files