laravel:count():参数必须是实现Countable

时间:2018-12-08 21:25:35

标签: php laravel laravel-5.3

我正在使用Laravel 5.3,而我的php ver是7.1

当我调用SoftDeletes类时,出现该错误

Builder.php第1231行中的

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

这是我的模特

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

这是我的控制器

<?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)
    {
        //
    }
}

当我删除count()函数时,也会出现相同的错误

我该如何解决该错误

3 个答案:

答案 0 :(得分:0)

Laravel 5.3和我的PHP ver是7.1不兼容 Refer to this issue in the github

要解决此错误,您可以做两件事

  • 将laravel 5.3升级到laravel 5.5时请参考(5.3 to 5.45.4 to 5.5
  • 将您的php降级到php 5.6

答案 1 :(得分:0)

laravel find()方法返回对象而不是数组时,我遇到了同样的问题。因此count()方法将不起作用。尝试: Stackoverflow解决方案或

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

将返回将与count()方法一起使用的数组。

答案 2 :(得分:-1)

我更改了C:\laragon\www\mystore\vendor\laravel\framework\src\Illuminate\Database\Eloquent中的第1231行

$originalWhereCount = !empty($query->wheres) ? count($query->wheres) : 0;
相关问题