簡體   English   中英

流明count():參數必須是實現Countable的數組或對象

[英]Lumen count(): Parameter must be an array or an object that implements Countable

我嘗試創建帶有帖子的多圖像上傳,但是當我嘗試對數組中的元素進行計數時,我經常遇到count函數錯誤。 對於單個圖像,如果刪除該數組並計數,則代碼可以正常工作。 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']);
    }
}

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()調用使用此技巧

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM