首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Lumen count():参数必须是实现Countable的数组或对象

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

Stack Overflow用户
提问于 2019-03-19 14:29:48
回答 1查看 1.5K关注 0票数 0

我尝试在发布的同时创建多个图像上传,但是当我尝试计算数组中的元素时,我总是在count函数上得到错误。对于单个图像,如果我删除该数组并进行计数,代码就可以正常工作。Lumen的最新版本是PHP 7.3版,但我将其降级到了7.1。

代码:

代码语言:javascript
复制
<?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']);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-19 18:40:09

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

试着改变

代码语言:javascript
复制
count($image_array)

代码语言:javascript
复制
count((is_countable($image_array)?$image_array:[]))

并对所有count()调用使用此技巧

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55234889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档