php - 更改对象属性值在 View 中返回 null

标签 php laravel orm

我正在尝试更改我的 account 对象的一些属性值。我正在尝试为 domainpassword

设置一个值

当我在将对象发送到前端(使用 vue)之前记录对象时,对象似乎已根据我的需要进行了修改,但是当我在 console.log()浏览器属性值为空。

我正在使用 Laravel excel 创建帐户。创建帐户后,我将事件与监听器一起使用,以使用推送器将我的帐户对象发送到我的 View 。

我该如何正确执行此操作?

帐户导入

class AccountsImport implements ToCollection, withHeadingRow
{
    use Importable;

    private $data;

    public function __construct(array $data = [])
    {
        $this->data = $data;
    }

    public function collection(Collection $rows)
    {
        $rows->each(function ($row, $key) {
           $account = Account::create(array_merge([
                'name' => mb_convert_encoding($row['student'], "UTF-8", mb_detect_encoding($row['student'], "UTF-8, ISO-8859-1, ISO-8859-15", true)),
                'email' => $row['school_e_mailadres'],
            ], $this->data));
            event(new AccountCreation($account));
        });
    }
}

AccountCreation 事件被触发,SetHostingAccount 监听器将运行

public function handle(AccountCreation $event) 
{
    $generator = new ComputerPasswordGenerator();
    $generator->setUppercase()->setLowercase()->setNumbers()->setSymbols(false)->setLength(20);
    $password = $generator->generatePassword();

    $domain = preg_replace('/\s+/', '.', mb_strtolower($event->account->name . ' mtantwerp.eu'));
    $event->account->{"domain"} = $domain;
    $event->account->{"password"} = $password;

    \Log::debug($event->account);

    return $event->account; 
}

Vue

 created() {
    Echo.channel("account-channel").listen("AccountCreation", e => {
      console.log(e.account);
      this.accounts.unshift(e.account);
    });      
  }

记录结果

\Log::debug($event->account) 的示例结果

{"name":"John Doe","email":"john.doe@student.kdg.be","package":"KDG student","id":577,"domain":"john.doe.mtantwerp.eu","password":"r5RSEvQBYnTF7RkPtL8Y"}

Console.log(e.account) 可见

Object
domain: null
email: "john.doe@student.kdg.be"
id: 703
name: "Doe John"
package: "KDG student"
password: null
status: "created"

最佳答案

您似乎没有将数据持久保存到模型中。

public function handle(AccountCreation $event) 
{
    $generator = new ComputerPasswordGenerator();
    $generator->setUppercase()->setLowercase()->setNumbers()->setSymbols(false)->setLength(20);
    $password = $generator->generatePassword();

    $domain = preg_replace('/\s+/', '.', mb_strtolower($event->account->name . ' mtantwerp.eu'));
    $event->account->{"domain"} = $domain;
    $event->account->{"password"} = $password;

    // persist 
    $event->account->save();

    return $event->account; 
}

编辑:我认为有两种方法可以处理这个问题。

要么您在更新帐户详细信息后重新广播该事件:

public function handle(AccountCreation $event) 
{
    $generator = new ComputerPasswordGenerator();
    $generator->setUppercase()->setLowercase()->setNumbers()->setSymbols(false)->setLength(20);
    $password = $generator->generatePassword();

    $domain = preg_replace('/\s+/', '.', mb_strtolower($event->account->name . ' mtantwerp.eu'));
    $event->account->{"domain"} = $domain;
    $event->account->{"password"} = $password;

    // persist 
    $event->account->save();

    // broadcast will have updated account details 
    // after it was persisted
    broadcast($event);

    return $event->account; 
}

在这种情况下,您还需要检查帐户是否在 Vue 组件上有域。

if(e.account.domain) {
    this.accounts.unshift(e.account);
}

这应该有效。但是,我宁愿您考虑创建一个新事件,以更好地描述您要执行的操作。 在这种情况下,帐户正在更新。因此,您将创建一个新的 AccountWasUpdated 事件,并且您将让 Echo 监听它。

public function handle(AccountCreation $event) 
{
    $generator = new ComputerPasswordGenerator();
    $generator->setUppercase()->setLowercase()->setNumbers()->setSymbols(false)->setLength(20);
    $password = $generator->generatePassword();

    $domain = preg_replace('/\s+/', '.', mb_strtolower($event->account->name . ' mtantwerp.eu'));
    $event->account->{"domain"} = $domain;
    $event->account->{"password"} = $password;

    // persist 
    $event->account->save();

    event(new AccountWasUpdated($event->account)); 

    return $event->account; 
}

Echo 不会监听 AccountCreation 事件,而是监听 AccountWasUpdated 事件,它将包含新的帐户对象。

关于php - 更改对象属性值在 View 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53987645/

相关文章:

使用 .htaccess 处理 PHP 错误

php - 获取触发 "Failed calling MyClass::jsonSerialize()"异常的异常

php - 服务器负载数据库与文件操作

mysql - 如何从专用 'fields' 表中选择值?

php - MYSQL 获取最近的记录,然后在 where 子句中验证它的状态

php - Laravel Scheduler 以 root 用户身份创建缓存文件

php artisan 制作:auth does not create auth controllers after deleting

javascript - Axios ReactJs Laravel : How to retrieve the multiple request Api

python - 组织包含 SQLAlchemy 模型的文件夹的最佳方式

java - 从现有数据库生成 JDO 对象