简体   繁体   English

如何在 Laravel 中将值存储为 integer

[英]How to store value as integer in Laravel

I'm developing a blog using Laravel and I'm trying to use the BenSampo Enum package.我正在使用 Laravel 开发博客,并且正在尝试使用 BenSampo Enum package。 Everything seems to work when saving a blog post but the issue arises when casting the post_status field as an integer.保存博客文章时似乎一切正常,但将 post_status 字段转换为 integer 时出现问题。 It is being cast as a string, though I've specifically set the cast as an integer on the model.它被转换为字符串,尽管我在 model 上专门将转换设置为 integer。 So I get the output below instead:所以我得到了下面的 output :

Cannot construct an instance of PostStatusEnum using the value (string) 1 .无法使用值(字符串) 1构造 PostStatusEnum 的实例。 Possible values are [0, 1, 2, 3, 4].可能的值为 [0, 1, 2, 3, 4]。

I did quite a bit of research online and found a trait that would make sure to return the cast as an integer.我在网上做了很多研究,发现了一个可以确保将演员阵容返回为 integer 的特征。 I modified it for this purpose and it indeed works as intended but it collides with the BenSampo\Enum\Traits\CastsEnums trait.我为此目的对其进行了修改,它确实按预期工作,但它与 BenSampo\Enum\Traits\CastsEnums 特征冲突。

When creating the post, I'm attaching the status via an observer based on a certain condition, it is not coming through a form, so I can't really validate it (haven't found a way yet, at least).创建帖子时,我根据特定条件通过观察者附加状态,它不是通过表单来的,所以我无法真正验证它(至少还没有找到方法)。 I basically want to do $post_status = $post->post_status on the controller and get whichever one was saved (or whichever the model has at a given time).我基本上想在 controller 上执行 $post_status = $post->post_status 并获取保存的任何一个(或 model 在给定时间拥有的任何一个)。 So, instead of trying to hack that package, I'm now trying to create a mutator to make sure the post_status field gets saved as an integer to start with but I'm having a bit of difficulty with that as well.因此,我现在没有尝试破解 package,而是尝试创建一个 mutator 以确保将 post_status 字段保存为 integer 开始,但我也遇到了一些困难。 The code below doesn't seem to do anything, what am I missing?下面的代码似乎没有做任何事情,我错过了什么?

public function setPostStatusAttribute($value)
    {
       $this->attributes['post_status'] = integer($value);
    }

For reference, this is what I have on the model and migration, have also imported the required namespaces:作为参考,这是我在 model 和迁移上的内容,还导入了所需的命名空间:

protected $enumCasts = [

     'post_status' => PostStatusEnum::class,
 ];

protected $casts = [
   'post_status' => 'int', // I've also tried integer. Have also tried putting it above the 
$enumCasts
 ];

//migration 
$table->unsignedInteger('post_status'); //have also tried integer and tinyInteger.

I also tried the Spatie Enums package, with the Laravel extension, that one returns the value as an integer but also ran into issues with that one trying to display the field in the show method.我还尝试了 Spatie Enums package,扩展名为 Laravel,它以 integer 的形式返回值,但在尝试显示的字段中也遇到了问题。

 $this->attributes['post_status'] = integer($value);

I don't know if this should even work, as far as I know, there isn't an integer(...) helper.我不知道这是否应该工作,据我所知,没有integer(...)助手。

Try casting instead:尝试转换:

$this->attributes['post_status'] = (int) $value;

I haven't used the first package that you mentioned, but the spatie/laravel-enum yes.我没有使用你提到的第一个 package,但是spatie/laravel-enum是的。 In that package (that, in turn, uses spatie/enum underneath) you can just do this:在那个 package (反过来,在下面使用spatie/enum )你可以这样做:

# Post.php

protected $enums = [
    'post_status' => PostStatusEnum::class,
    'another_field'   => AnotherFieldEnum::class . ':nullable', 
    //    the last bit is to accept nullable values ^^^^^^^^^
];

In case you want to use accessors:如果您想使用访问器:

public function getPostStatusAttribute($postStatus)
{
    return PostStatusEnum::make($postStatus);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM