Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing object property values returns null in view

Tags:

php

orm

laravel

I'm trying to change some property values of my account object. I'm trying to set a value on domain and password

When I log my object before I send it over to my front end (using vue), the object seems to be modified to my needs, but when I console.log() it in the browser the property values are null.

I'm using Laravel excel to create the accounts. After an account is created I use events with listeners to send my account objects to my view with pusher.

How do I do this properly?

AccountsImport

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 event gets fired and SetHostingAccount listener will run

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 results

Example result of \Log::debug($event->account)

{"name":"John Doe","email":"[email protected]","package":"KDG student","id":577,"domain":"john.doe.mtantwerp.eu","password":"r5RSEvQBYnTF7RkPtL8Y"}

Console.log(e.account) in view

Object
domain: null
email: "[email protected]"
id: 703
name: "Doe John"
package: "KDG student"
password: null
status: "created"
like image 336
Dax Avatar asked Jun 07 '26 19:06

Dax


1 Answers

It seems you are not persisting your data to the model.

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; 
}

Edit: I think there are two ways you can handle this.

Either you re-broadcast the event after updating your account details:

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; 
}

In this case, you would also need to check for whether the account has a domain on the Vue component.

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

This should work. However, I'd rather you consider creating a new event which will better describe what you are tring to do. In this case, the account is being updated. So you would create a new AccountWasUpdated event and you will have Echo listen to it instead.

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; 
}

Instead of having Echo listen to the AccountCreation event, it would listen to the AccountWasUpdated event and it will contain the new Account Object.

like image 93
Mozammil Avatar answered Jun 09 '26 08:06

Mozammil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!