简体   繁体   English

Laravel后端未保存到数据库

[英]Laravel backend not saving to database

I'm uploading files using laravel as backend API REST. 我正在使用laravel作为后端API REST上传文件。 When I upload a file in the frontend, it sends a 200 OK status code in the response, but doesn't save it to the database. 当我在前端上传文件时,它会在响应中发送200 OK状态代码,但不会将其保存到数据库中。 Something weird that I see it's that in the response the data comes empty, and the array of the files comes in the config section of the response. 我发现有些奇怪的是,响应中的数据为空,文件数组位于响应的config部分。 200 OK响应

This is the controller that handles this situation 这是处理这种情况的控制器

 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);
    }

This is my form 这是我的表格

<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>

So you're getting an empty response because your conditional statement isn't matching, so you're just returning that empty $fileArray you created: 因此,由于条件语句不匹配,您将得到一个空响应,因此您只是返回创建的空$fileArray

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

The correct way to check if there are files is to go: 检查文件是否正确的正确方法是:

if($request->hasFile('file'))

Everything you need to know about this is available in the official documentation here: 您需要了解的所有信息都可以在以下官方文档中找到:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM