4

I'm using Laravel 5.3 and my php ver is 7.1

when i called SoftDeletes class i get that error

ErrorException in Builder.php line 1231: count(): Parameter must be an array or an object that implements Countable

this is my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class Post extends Model
{

    use SoftDeletes;

    protected $dates = ['deleted_at'];

    protected $fillable = [

        'title','content','image','category_id','slug'
    ];



    public function category(){


        return $this->belongsTo('App\Category');
    }
}

and it is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;

use App\Category;

use Session;

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {



        return view('admin.posts.index')->with('posts',Post::all());

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $category = Category::all();

        if($category->count() == 0){

            Session::flash('info' , 'You must create at least 1 category to add a new post');

            return redirect()->back();
        }

        return view('admin.posts.post')->with('categories',$category);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $this->validate($request,[

            'title'         => 'required|max:255',
            'image'         => 'required|image',
            'content'       => 'required',
            'category_id'   => 'required'
        ]);


        $image = $request->image;

        $image_new_name = time().$image->getClientOriginalName();

        $image->move('/uploads/posts' , $image_new_name);



        $post= Post::create([

            'title'          => $request->title,
            'image'          => '/uploads/posts/' . $image_new_name,
            'content'        => $request->content,
            'category_id'    => $request->category_id,
            'slug'           => str_slug($request->title)
        ]);

        Session::flash('success' , 'You created a new post');

        return redirect()->back();



    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

and when i delete the count() function also get the same error

how can i solve this error

  • You'll need to look deeper, the problem is not with the code you posted. The error message should indicate a file name and line number, where `count()` is called. To help you debug this, we need to know what is being passed to `count()` and where it came from. – rickdenhaan Dec 08 '18 at 21:38

3 Answers3

4

Laravel 5.3 and my PHP ver is 7.1 is not compatible with each other Refer to this issue in the github

To solve this error you can do two things

  • Upgrade laravel 5.3 to laravel 5.5 refer this ( 5.3 to 5.4, 5.4 to 5.5 )
  • Downgrade you php to php 5.6
Vikas Rinvi
  • 916
  • 6
  • 26
0

I changed line 1231 in C:\laragon\www\mystore\vendor\laravel\framework\src\Illuminate\Database\Eloquent

to

$originalWhereCount = !empty($query->wheres) ? count($query->wheres) : 0;
akshaypjoshi
  • 1,180
  • 1
  • 11
  • 22
  • Changing the code in the vendor directory is never a good idea. - These changes will vanish once you run composer update or install command - These kinds of changes can bring you very unexpected behaviour in the application - Trust me this is not an optimal solution as it will break the code in another area ``` I personally suggest to check your laravel and php dependency and upgrade PHP/laravel which is supported. In this case. Php 5.6 is supported not PHP 7.2. ``` – Vikas Rinvi Apr 27 '19 at 10:20
  • In our situation, with an old project, it was our only solution until we could upgrade the php and laravel version. – Diego Galocha Aug 18 '20 at 09:11
0

I had the same problem with the laravel find() method returning an object and not an array. Therefore the count() method will not work. Try: Stackoverflow solution or

Post::all()->toArray();

Will return an array that will work with the count() method.

Hmerman6006
  • 606
  • 6
  • 17