Hi i have problem in saving data when my models are related. i have to tables: admin and admin_token
for admin:
public $id;
public $username;
public $password;
public $logged_in;
public function initialize()
{
$this->setSource("admin");
$this->hasMany(
'id',
'App\\Models\\AdminTokens',
'admin_id',
[
'alias' => 'Tokens',
]
);
}
and for admin_token:
public $id;
public $admin_id;
public $is_revoked;
public function initialize()
{
$this->setSource("admin_tokens");
$this->belongsTo(
'admin_id',
'App\\Models\\Admin',
'id',
[
"alias" => "Admin",
]
);
}
now in controller something like blow wont work:
$admin = Admin::findFirstByUsername($username);
foreach( $admin->tokens as $token ) {
$token->is_revoked = 1;
}
$tokens = [];
$tokens[0] = new AdminTokens();
$tokens[0]->admin_id = $admin->id;
$tokens[0]->is_revoked = 0;
$admin->tokens = $tokens;
$admin->logged_in = 1;
$admin->save()
is there any idea why this method not only update token and set them to 1 but also it does not save any token in admin_tokens table?