【问题标题】:Single post works but looped post returns 500 error单个帖子有效,但循环帖子返回 500 错误
【发布时间】:2019-12-02 15:57:10
【问题描述】:

当我一张一张添加图片时,它会保存并返回 200 响应。但是当我尝试将其转换为循环时,它会返回 500 错误。图像确实保存到 s3 的两种方式,所以我不确定 500 错误来自哪里

postsController@store 中的工作代码

        $uploadedFile = request()->images[0];
        $uploadedFile->store('uploads', 's3');
        return['hi', request('images')];

非工作循环

          foreach(request('images') as $image){
            $imagePath= $image->store('uploads', 's3');

        };
        return 'hi';

我将对象称为 request('images') 和 request()->images,我认为两者是相同的并且应该产生相同的输出。

如果我点击网络选项卡中的错误,我会得到这个

    "message": "Call to a member function store() on null",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "/Users/raj/challenges/instagrizzleTWC/instagrizzle/app/Http/Controllers/PostsController.php",
    "line": 25,
    "trace": [
        {
            "function": "store",
            "class": "App\\Http\\Controllers\\PostsController",
            "type": "->"
        },

这是我来自 vue 的数据对象

        data: function () {
            return{
                image_count:1,
                max_uploads:5,
                totalsteps:4,
                step:1,
                images:[
                    {image_path:"", image_filters:[],},
                    {image_path:"", image_filters:[],},
                    {image_path:"", image_filters:[],},
                    {image_path:"", image_filters:[],},
                    {image_path:"", image_filters:[],},
                ],
                caption:"new post",
                starting_bid:"10",
                bid_increment:"10",
                end_time:"11-30-2019 12:11:00",
                bin:"100",
                snipe:true,
                snipe_time:"5",
                autoremove_bids:true,
                csrf: document.querySelector('meta[name="csrf-token"]').getAttribute('content'),

            }
        },

这是我创建 formData 并发送到后端的提交方法

            handleSubmit: function()
            {
                const config = { headers: { 'Content-Type': 'multipart/form-data'}}
                const post_data = new FormData();
                post_data.append('images[]', this.images[0]['image_path']);
                post_data.append('images[]', this.images[1]['image_path']);
                post_data.append('images[]', this.images[2]['image_path']);
                post_data.append('images[]', this.images[3]['image_path']);
                post_data.append('images[]', this.images[4]['image_path']);

                axios.post('/p', post_data, config)
                  .then(function (response) {
                    console.log('response', response);
                  })
                  .catch(function (error) {
                    console.log(error);
                  });
            },

我认为这可能与 csrf 令牌有关,但原始帖子应该会失败?

【问题讨论】:

  • 它会保存所有图像吗?那么我的猜测是最后有一个空的images[] 元素。 dd(request('images')); 输出什么?
  • aaah 嗯,可能就是这样。因此,由于某种原因,我遇到的另一个问题 dd() 不能像使用刀片模板那样工作。我以为可能是和vue冲突?
  • 是的,就是这样,谢谢托马斯!
  • 乐于助人。

标签: php laravel vue.js


【解决方案1】:

Images 正在尝试发布空对象,添加 if 语句:

        $image_arr = [];

        foreach(request('images') as $image){
            if($image != null){
                $imagePath = $image->store('uploads', 's3');
                array_push($image_arr, $imagePath);
            }

        };
        return ['Image Array', $image_arr];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-26
    相关资源
    最近更新 更多