Eloquent Laravel 模型上的 __construct

A __construct on an Eloquent Laravel Model(Eloquent Laravel 模型上的 __construct)
本文介绍了Eloquent Laravel 模型上的 __construct的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个自定义的 setter,我在我的模型的 __construct 方法中运行它.

I have a custom setter that I'm running in a __construct method on my model.

这是我要设置的属性.

    protected $directory;

我的构造函数

    public function __construct()
    {
        $this->directory = $this->setDirectory();
    }

二传手:

    public function setDirectory()
    {
        if(!is_null($this->student_id)){
            return $this->student_id;
        }else{
            return 'applicant_' . $this->applicant_id;
        }
    }

我的问题是在我的 setter 中,$this->student_id(这是从数据库中提取的模型的一个属性)返回 null.当我在我的 setter 中 dd($this) 时,我注意到我的 #attributes:[] 是一个空数组.
所以,直到 __construct() 被触发后,模型的属性才会被设置.如何在构造方法中设置 $directory 属性?

My problem is that inside my setter the, $this->student_id (which is an attribute of the model being pulled from the database) is returning null. When I dd($this) from inside my setter, I notice that my #attributes:[] is an empty array.
So, a model's attributes aren't set until after __construct() is fired. How can I set my $directory attribute in my construct method?

推荐答案

您需要将构造函数更改为:

You need to change your constructor to:

public function __construct(array $attributes = array())
{
    parent::__construct($attributes);

    $this->directory = $this->setDirectory();
}

第一行 (parent::__construct()) 会在你的代码运行之前运行 Eloquent Model 自己的构造方法,这将设置所有的属性为你.此外,对构造函数方法签名的更改是继续支持 Laravel 期望的用法: $model = new Post(['id' => 5, 'title' => 'My Post']);

The first line (parent::__construct()) will run the Eloquent Model's own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor's method signature is to continue supporting the usage that Laravel expects: $model = new Post(['id' => 5, 'title' => 'My Post']);

经验法则实际上是始终记住,在扩展类时,要检查您没有覆盖现有方法以使其不再运行(这对于神奇的 __construct__get 等方法).您可以检查原始文件的来源,看看它是否包含您正在定义的方法.

The rule of thumb really is to always remember, when extending a class, to check that you're not overriding an existing method so that it no longer runs (this is especially important with the magic __construct, __get, etc. methods). You can check the source of the original file to see if it includes the method you're defining.

这篇关于Eloquent Laravel 模型上的 __construct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)