Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Model Context not used a second time #26

Closed
ismaail opened this issue Jul 13, 2023 · 4 comments
Closed

User Model Context not used a second time #26

ismaail opened this issue Jul 13, 2023 · 4 comments
Labels
bug Something isn't working stale

Comments

@ismaail
Copy link

ismaail commented Jul 13, 2023

Laravel Settings

v2.2.2

Laravel Version

v10.15.0

Bug description

We have a array of settings to save per User

$input = [
  'api_key' => 'AAAA',
  'api_secret' => 'BBBB',
];

The first one api_key gets saved and the Context here is
the Model User Class Name & the User's id

The second one api_secret is also saved, but the Context is NULL

The result is api_secret gets shared by all Users!

Steps to reproduce

  1. Add HasSettings Trait to the User Model

  2. Log-in so we can has a User Model as the Context

  3. prepare some settings to save (from a request mostly)

$input = [
  'api_key' => 'AAAA',
  'api_secret' => 'BBBB',
];

Save the Settings

$settings = \Auth::user()->settings();
// or $settings = $request->user()->settings();

foreach ($input as $key => $value) {
  $settings->set($key, $value);
}

Relevant log output

No response

@ismaail ismaail added the bug Something isn't working label Jul 13, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Aug 4, 2023

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Aug 4, 2023
@ismaail
Copy link
Author

ismaail commented Aug 4, 2023

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days.

bad bot

@github-actions github-actions bot removed the stale label Aug 5, 2023
@github-actions
Copy link
Contributor

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@rawilk
Copy link
Owner

rawilk commented Sep 27, 2023

The reason why the context is lost on your model in that foreach loop is because the underlying settings service resets the context after each call. For this reason, I would just call it like this in your loop:

foreach ($input as $key => $value) {
    auth()->user()->settings()->set($key, $value);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working stale
Projects
None yet
Development

No branches or pull requests

2 participants