流明count():参数必须是实现Countable的数组或对象

时间:2019-03-19 06:29:48

标签: php laravel-5 lumen

我尝试创建带有帖子的多张图片,但是当我尝试对数组中的元素进行计数时,我不断遇到计数函数错误。对于单个图像,如果删除该数组并计数,则代码可以正常工作。 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']);
    }
}

1 个答案:

答案 0 :(得分:0)

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()调用

相关问题