laravel : count(): Parameter must be an array or a

2019-07-25 11:46发布

问题:

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

回答1:

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


回答2:

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;