简体   繁体   English

为什么在我的 laravel 应用程序中没有调用访问器?

[英]Why Accessors in not called in my laravel app?

I was not happy with the default created_at date format in my database table so I decided to create Accessors which will simply change my date format.我对数据库表中默认的 created_at 日期格式不满意,所以我决定创建访问器,它只会更改我的日期格式。 But I found out that Accessors is not running.但是我发现访问器没有运行。 I tried to use it on another column but it doesn't change my output. then I use dd($value) so it can stop in accessor but I found out that it didn't.我试图在另一列上使用它,但它没有改变我的 output。然后我使用 dd($value) 所以它可以在访问器中停止,但我发现它没有。 I am getting my data from the database and the accessor is been bypassed.我正在从数据库中获取数据并且访问器被绕过了。

my accessor in model class is:我在 model class 中的访问器是:

 public function getCreatedAtAttribute($value)
    {
        dd($value);
    }

But it does not effect anything as I am getting my output of:但这对我的 output 没有任何影响:

$case=Allcase::with('donner')->get();
        print_r($case);

and also no effect to created_at:对 created_at 也没有影响:

在此处输入图像描述

You can customize the actual attribute value in this way您可以通过这种方式自定义实际属性值

public function getCreatedAtAttribute()
{
    return Carbon::parse($this->attributes['created_at'])->format('Y-m-d');

} 

Now, you can get your data and print it.现在,您可以获取数据并进行打印。 You can show the differences now您现在可以显示差异

$case = Allcase::with('donner')->get();
print_r($case);
    

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

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