gpt4 book ai didi

php - Laravel : count(): Parameter must be an array or an object that implements Countable

转载 作者:行者123 更新时间:2023-12-03 23:14:28 24 4
gpt4 key购买 nike

我使用的是 Laravel 5.3,我的 php 版本是 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');
}
}

这是我的 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)
{
//
}
}

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

我该如何解决这个错误

最佳答案

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

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

  • 将 laravel 5.3 升级到 laravel 5.5 引用此 ( 5.3 to 5.4 , 5.4 to 5.5 )
  • 将您的 php 降级到 php 5.6
  • 关于php - Laravel : count(): Parameter must be an array or an object that implements Countable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53687128/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com