lundi 18 mars 2019

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

I tried creating multiple image upload along with a post but I constantly get an error on count function when I try to count the elements in the array. for the single image, the code works fine if I remove that array and counts. Lumen v latest, PHP version 7.3 but I downgraded it to 7.1. Any possible solutions. Given below is my code:-

    <?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']);
        }
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire