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
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
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;
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.
As a workaround add this to your routes file:
if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.