当我尝试用Laravel8.41.0和PHP8.0.3在我的应用程序mede中签名时,我遇到了错误。
错误
TypeError
count(): Argument #1 ($value) must be of type Countable|array, string given
位置
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php:235
文件
<?php
namespace Illuminate\Database\Eloquent\Concerns;
use Illuminate\Support\Str;
trait GuardsAttributes
{
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [];
/**
* The attributes that aren't mass assignable.
*
* @var string[]|bool
*/
protected $guarded = ['*'];
/**
* Indicates if all mass assignment is enabled.
*
* @var bool
*/
protected static $unguarded = false;
/**
* The actual columns that exist on the database and can be guarded.
*
* @var array
*/
protected static $guardableColumns = [];
/**
* Get the fillable attributes for the model.
*
* @return array
*/
public function getFillable()
{
return $this->fillable;
}
/**
* Set the fillable attributes for the model.
*
* @param array $fillable
* @return $this
*/
public function fillable(array $fillable)
{
$this->fillable = $fillable;
return $this;
}
/**
* Merge new fillable attributes with existing fillable attributes on the model.
*
* @param array $fillable
* @return $this
*/
public function mergeFillable(array $fillable)
{
$this->fillable = array_merge($this->fillable, $fillable);
return $this;
}
/**
* Get the guarded attributes for the model.
*
* @return array
*/
public function getGuarded()
{
return $this->guarded === false
? []
: $this->guarded;
}
/**
* Set the guarded attributes for the model.
*
* @param array $guarded
* @return $this
*/
public function guard(array $guarded)
{
$this->guarded = $guarded;
return $this;
}
/**
* Merge new guarded attributes with existing guarded attributes on the model.
*
* @param array $guarded
* @return $this
*/
public function mergeGuarded(array $guarded)
{
$this->guarded = array_merge($this->guarded, $guarded);
return $this;
}
/**
* Disable all mass assignable restrictions.
*
* @param bool $state
* @return void
*/
public static function unguard($state = true)
{
static::$unguarded = $state;
}
/**
* Enable the mass assignment restrictions.
*
* @return void
*/
public static function reguard()
{
static::$unguarded = false;
}
/**
* Determine if the current state is "unguarded".
*
* @return bool
*/
public static function isUnguarded()
{
return static::$unguarded;
}
/**
* Run the given callable while being unguarded.
*
* @param callable $callback
* @return mixed
*/
public static function unguarded(callable $callback)
{
if (static::$unguarded) {
return $callback();
}
static::unguard();
try {
return $callback();
} finally {
static::reguard();
}
}
/**
* Determine if the given attribute may be mass assigned.
*
* @param string $key
* @return bool
*/
public function isFillable($key)
{
if (static::$unguarded) {
return true;
}
// If the key is in the "fillable" array, we can of course assume that it's
// a fillable attribute. Otherwise, we will check the guarded array when
// we need to determine if the attribute is black-listed on the model.
if (in_array($key, $this->getFillable())) {
return true;
}
// If the attribute is explicitly listed in the "guarded" array then we can
// return false immediately. This means this attribute is definitely not
// fillable and there is no point in going any further in this method.
if ($this->isGuarded($key)) {
return false;
}
return empty($this->getFillable()) &&
strpos($key, '.') === false &&
! Str::startsWith($key, '_');
}
/**
* Determine if the given key is guarded.
*
* @param string $key
* @return bool
*/
public function isGuarded($key)
{
if (empty($this->getGuarded())) {
return false;
}
return $this->getGuarded() == ['*'] ||
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded())) ||
! $this->isGuardableColumn($key);
}
/**
* Determine if the given column is a valid, guardable column.
*
* @param string $key
* @return bool
*/
protected function isGuardableColumn($key)
{
if (! isset(static::$guardableColumns[get_class($this)])) {
static::$guardableColumns[get_class($this)] = $this->getConnection()
->getSchemaBuilder()
->getColumnListing($this->getTable());
}
return in_array($key, static::$guardableColumns[get_class($this)]);
}
/**
* Determine if the model is totally guarded.
*
* @return bool
*/
public function totallyGuarded()
{
return count($this->getFillable()) === 0 && $this->getGuarded() == ['*'];
}
/**
* Get the fillable attributes of a given array.
*
* @param array $attributes
* @return array
*/
protected function fillableFromArray(array $attributes)
{
if (count($this->getFillable()) > 0 && ! static::$unguarded) {
return array_intersect_key($attributes, array_flip($this->getFillable()));
}
return $attributes;
}
}
有些发问者问同样的问题,我看到在最新版本的PHP中出现了错误。但我不知道如何解决这个问题,所以请告诉我,在我的文件中的位置和如何修复。
谢谢你的回应!!
$fillable是用方括号声明的,上面添加了所有文件。我怎么修改它呢?
model.php (仅可填入相关)
public function fill(array $attributes)
{
$totallyGuarded = $this->totallyGuarded();
foreach ($this->fillableFromArray($attributes) as $key => $value) {
// The developers may choose to place some attributes in the "fillable" array
// which means only those attributes may be set through mass assignment to
// the model, and all others will just get ignored for security reasons.
if ($this->isFillable($key)) {
$this->setAttribute($key, $value);
} elseif ($totallyGuarded) {
throw new MassAssignmentException(sprintf(
'Add [%s] to fillable property to allow mass assignment on [%s].',
$key, get_class($this)
));
}
}
return $this;
}
app模型user.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class user extends Model
{
use HasFactory;
protected $fillable = 'name';
}
发布于 2021-09-04 12:03:06
三次检查$this->getFillable()
(第235行)是否给出了一个数组。
我敢打赌,您可能只是在声明$fillable
属性时忘记了模型中的方括号。
在您的模型中,应该如下所示:
protected $fillable = [
'attribute_a',
'attribute_b',
'attribute_c',
//...
];
TypeError是PHP8.0中引入的一种新型错误。顾名思义,当给函数的类型不是PHP所期望的类型时,就会抛出它。
在您的例子中,count()
需要一个数组,但是由于模型的$fillable
属性很可能是一个简单的字符串,所以会抛出一个TypeError。
PHP版本之间的比较:
< 8.0
count('helloworld'); // 1
>= 8.0
count('helloworld'); // TypeError
发布于 2022-02-07 21:03:11
您可以用isset()交换count()
发布于 2022-08-02 11:45:27
我们在客户的Drupal网站上也遇到了同样的问题。它的论坛模块不会加载,并给出了这个错误。
这个问题源于其中一个用户发布的.svg文件。我们回顾了管理面板中的评论,并在几个小时前看到了一条评论,随后是一个.svg文件。Drupal无法加载该文件,并将其替换为
这就是我们如何意识到这是问题所在。我们认为,这只是在帖子指向论坛第2页时引起问题的原因。
移除SVG解决了这个问题。我们并不打算找到并修复导致这一问题的部分,相反,我们计划阻止在论坛中添加SVG文件。这就是我们解决问题的方法。希望它能帮到别人。
https://stackoverflow.com/questions/69055075
复制相似问题