【问题标题】:Lumen count(): Parameter must be an array or an object that implements CountableLumen count():参数必须是数组或者实现了Countable的对象
【发布时间】:2019-03-19 06:29:48
【问题描述】:

我尝试在帖子中创建多张图片上传,但是当我尝试对数组中的元素进行计数时,计数函数经常出错。对于单个图像,如果我删除该数组并计数,则代码可以正常工作。 Lumen 最新版本,PHP 版本 7.3 但我将其降级为 7.1。

代码:

<?php
class PostController extends Controller {

    public function __construct() {
        $this->middleware('auth');
    }

    public function index() {
        return Auth::user()->post;
    }

    public function show($post_id) {
        $post = Post::findOrFail($post_id);
        if (Auth::user()->id !== $post->user_id) {
            return response()
                ->json(['status' => 'error', 'message' => 'unauthorized'], 401);
        }
        return $post;
    }

    public function store(Request $request) {
        $post = new Post;
        $post->setConnection('mysql1');
        $user = User::where('token', $request->token)
            ->first();
        if ($user) {
            $post1 = $post->create(['post_id' => rand() , 'title' => $request->title, 'cooked_time' => $request->cooked_time, 'user_id' => $request->token, 'location' => $user['name'], 'dispose_time' => strtotime('+01 hours', strtotime($request->cooked_time)) , 'food_type' => $request->food_type, 'description' => $request->description, 'serve_quantity' => $request->serve_quantity, 'lat' => $request->lat, 'long' => $request->long, ]);
            $post = Post::where('user_id', $request->token)
                ->orderBy('id', 'DESC',)
                ->limit('1')
                ->get();

            if ($request->hasFile('image')) {
                // $image_array = [];
                $image_array = $request->file('image');
                $array_len = count($image_array);

                for ($i = 0;$i < $array_len;$i++) {
                    $image_size = $image_array[$i]->getClientSize();
                    $image_ext = $image_array[$i]->getClientOriginalExtension();
                    $new_image_name = rand(123456, 99999999) . "." . $image_ext;
                    if (!function_exists('public_path')) {
                        /**
                         * Return the path to public dir
                         *
                         * @param null $path
                         *
                         * @return string
                         */
                        function public_path($path = null) {
                            return rtrim(app()->basePath('public/' . $path) , '/');
                        }
                    }
                    $destination_path = public_path('/images');
                    $image_array[$i]->move($destination_path, $new_image_name);
                    $image = new PostImage;
                    $images->image_name = $new_image_name;
                    $images->post_id = $post1['post_id'];
                    $images->save();
                }

            }
            return response()
                ->json(['message' => 'success', 'user' => $user['name'], 'post' => $post1, ], 200);
        }
    }

    public function update(Request $request, $post_id) {
        $post = Board::find($post_id);
        if (Auth::user()->id !== $post_id->user_id) {
            return response()
                ->json(['status' => 'error', 'message' => 'unauthorized'], 401);
        }
        $post->update($request->all());

        return response()
            ->json(['status' => 'success', 'post' => $post], 200);
    }

    public function destroy($id) {
        $post = Post::find($post_id);
        if (Auth::user()->id !== $post->user_id) {
            return response()
                ->json(['status' => 'error', 'message' => 'unauthorized'], 401);
        }
        if (Post::destroy($id)) {
            return response()->json(['status' => 'success', 'message' => 'Post Deleted Successfully'], 200);
        }
        return response()
            ->json(['status' => 'error', 'message' => 'Something Went Wrong']);
    }
}

【问题讨论】:

  • @karel 我正在使用文本编辑器,如果有任何意外的括号或错误,它会通知我
  • @karel 但这并没有给我带来问题,当我尝试传递图像时,问题在于计数功能
  • 表单是什么样子的?您目前正在寻找 $request-&gt;file('image') 这似乎不是一个数组。也许您的表单应该看起来像&lt;input type="file" class="form-control" name="images[]" ..&gt;。对您的 $request-&gt;file('image') 执行 var_dump 以检查您收到了哪些数据。
  • @BertMaurau 我只是在创建 API 我没有通过邮递员制作我正在测试的前端
  • @BertMaurau 我只是在创建 API 在你对 var_dump 说我试过之后我没有通过邮递员测试我的前端,这就是我得到的对象(Illuminate\Http\UploadedFile)#89 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) }

标签: php laravel-5 lumen


【解决方案1】:

http://php.net/manual/en/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types

尝试改变

count($image_array)

到

count((is_countable($image_array)?$image_array:[]))

并将这个技巧用于所有 count() 调用

【讨论】:

  • 还是一样的错误count(): Parameter must be an array or an object that implement Countable
  • 试试这个:count((is_countable($image_array)?$image_array:[]))
猜你喜欢
  • 2018-06-28
  • 2019-08-03
  • 2018-06-08
  • 2018-09-22
  • 2018-11-26
  • 2019-04-02
  • 2020-04-27
  • 2021-11-05
  • 2019-10-04
相关资源
最近更新 更多