GithubHelp home page GithubHelp logo

wildsideuk / laravel-userstamps Goto Github PK

View Code? Open in Web Editor NEW
504.0 13.0 58.0 42 KB

Laravel Userstamps provides an Eloquent trait which automatically maintains `created_by` and `updated_by` columns on your model, populated by the currently authenticated user in your application.

Home Page: https://wildside.uk

License: MIT License

PHP 100.00%
laravel laravel-package userstamps trait php

laravel-userstamps's People

Contributors

geisi avatar justinbyrne avatar mahmoudmohamedramadan avatar mattmcdonald-uk avatar mazyvan avatar romeromsk avatar stylecibot avatar tacone avatar tkayfun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-userstamps's Issues

Hard coded created_by implemented instead of getCreatedByColumn

Hi @mattmcdonald-uk,

Recently just began to use your package and saw something weird.. You allow the developers to change the created_by, updated_by etc columns by defining constants, but the belongsTo functionality is using a hard coded column name.

schermafbeelding 2018-07-18 om 17 18 07

I've changed it on my localhost to use the following:
schermafbeelding 2018-07-18 om 17 19 08

I guess it's something which went wrong, if yes than I'll make a PR right away to fix this problem.

Like to hear!

Cheers

It's not possible to define another 'creating' listener on 'userstamp' model

Hello there! Great extension, thank you 👍
But I think there is a problem. After applying Usersstamps trait one can no longer define his own creating (and I think updating as well) listeners.

For example, I have model

...
class Blabla extends Model
{
  use Userstamps;
}

and Observer

...
class BlablaObserver
{
  public function creating(Blabla $bla)
  {
     dd($bla);
  }
}

and Blabla::observe(BlablaObserver::class); in AppServiceProvider.

and creating is never fired in my observer; but saving for example is firing.

I think this is due to the fact you redefine creating function in yours registerListeners() function like this:
static::creating('Wildside\Userstamps\Listeners\Creating@handle');

Am I right, or am I do something wrong?

add userstamps to timestamps()

Can you add the created_by and updated_by fields to timestamps() so that all new tables created will include the userstamps fields

userstamps() in migrations

Adding a userstamps() method to be used in migrations could be useful.
For example:

...
$table->timestamps();
$table->userstamps();
$table->softDeletes();

Thank you!

Update doesn't work

I tried the following it doesn't work

  1. create new - it updates all including the updated_by & created_by
  2. I tried to update the record, i use "save()" or "update()" both no effect.
    Do you have any clue?

User.php
use Laratrust\Traits\LaratrustUserTrait;
use Wildside\Userstamps\Userstamps;

class User extends Authenticatable
{
use Notifiable;
use LaratrustUserTrait;
use Userstamps;

protected $fillable = [
    'name', 'email', 'password', 'created_at', 'updated_at', 'created_by', 'updated_by'
];

...

auth guard getprovider returns null

i am getting following error

Call to a member function getModel() on null
Symfony\Component\Debug\Exception\FatalThrowableError
...\vendor\wildside\userstamps\src\Userstamps.php

and error is thrown from


protected function getUserClass()
    {
        if (get_class(auth()) === 'Illuminate\Auth\Guard') {
            return auth() -> getProvider() -> getModel();
        }
        return auth() -> guard() -> getProvider() -> getModel();
    }

but when i replace eht auth()->gurad()
to
auth()->guard('web') it works

protected function getUserClass()
    {
        if (get_class(auth()) === 'Illuminate\Auth\Guard') {
            return auth() -> getProvider() -> getModel();
        }
        return auth() -> guard('web') -> getProvider() -> getModel();
    }

i am using laravel 5.5 and passport api authentication

Repo has no license

Would you mind including a license with the repo since you intend for people to use this code?

deleted_by not setted

Hi!, great package here.. Congrats..

I found a little issue.. when you delete a collections of items, the deleted_by field wont be set.

Example:

//This case fails
$users = User::all();
$users->delete();

The 'update' events are getting fired on deletion

I have this little issue.

I have a listener on the model updated event to send a mail. But I'm facing right now that the delete method first updated my record (to store the deleted_by field) and then proceed with the actual delete.

Check this logs for example

[2018-09-12 16:02:53] local.INFO: Model saving  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":null,"deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:16","updated_by":1}  
[2018-09-12 16:02:53] local.INFO: Model updating  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":null,"deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:16","updated_by":1}  
[2018-09-12 16:02:53] local.INFO: Model updated  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":null,"deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:53","updated_by":1}  
[2018-09-12 16:02:53] local.INFO: Model saved  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":null,"deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:53","updated_by":1}  
[2018-09-12 16:02:53] local.INFO: Model deleting  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":null,"deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:53","updated_by":1}  
[2018-09-12 16:02:53] local.INFO: Model deleted  
[2018-09-12 16:02:53] local.INFO: {"id":9,"total":"0.00","notas":null,"accepted_at":"2018-09-12 16:02:03","cotizable_id":0,"cotizable_type":"","cliente_id":null,"deleted_at":"2018-09-12 16:02:53","deleted_by":1,"created_at":"2018-09-12 16:01:58","created_by":1,"updated_at":"2018-09-12 16:02:53","updated_by":1}  

I've look at the code and I have a solution that has not issues with this #7 or rather the prevention of future events from being fired AFAIK

Two queries executed when deleting

Screen Shot 2019-10-15 at 22 24 48

$detail = UserDetails::find($request->id);
$detail->delete()

Im using laravel telescope for debugging, and I found that two queries are executed when deleting a record (SoftDeleted).

Laravel 5.8 with Homestead.
Package version 0.6.0

The user in the parent table is not updated when the child record is modified

The user in the parent table is not updated when the child record is modified.
For the field "updated_at" if the date is updated

In the model the child table defines the attribute "touches".

    protected $touches = ['persons'];

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */    
    public function persons()
    {
        return $this->belongsTo('App\Models\ContactPerson', 'contact_id', 'id');
    }

regards

suggestion, add user name

It is an excellent package to use in our projects. Thank you very much for your help.

I have the following suggestion, which makes it more practical to use the name of the user that I create, update or delete:

Add the following to the model:

protected $ appends = ['created_by_user', 'updated_by_user', 'deleted_by_user'];

Public function getCreatedByUserAttribute ()
    {
        if (! empty ($ this-> creator-> name)) {
            return $ this-> creator-> name;
        } else {
            return '';
        }
    }

    Public function getUpdatedByUserAttribute ()
    {
        if (! empty ($ this-> editor-> name)) {
            return $ this-> editor-> name;
        } else {
            return '';
        }
    }

    Public function getDeletedByUserAttribute ()
    {
        if (! empty ($ this-> destroyer-> name)) {
            return $ this-> destroyer-> name;
        } else {
            return '';
        }
    }

You could declare in the model which username to return
const USER_BY = 'name'; // const USER_BY = 'email';

Regards,

detach doesn't work with userstamps

When there is an belongstomany relationship, detach/attach methods doesn't fill user stamps fields.

For example;

Auth::user()->buyerDemandActions()->detach($args['buyer_demand_id']);

Doesn't fill deleted_by in the pivot table.

User class not found

Hi WildSideUK,

Tayfun here again!

I've found a bug in the getUserClass function, when using this via API.

I got the following error:

 "message": "Call to a member function getModel() on null",
    "exception": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
    "file": "/home/vagrant/code/dw-client-portal/vendor/wildside/userstamps/src/Userstamps.php",
    "line": 151,

The function (now):

protected function getUserClass()
    {
        if (get_class(auth()) === 'Illuminate\Auth\Guard') {
            return auth() -> getProvider() -> getModel();
        }

        return auth() -> guard() -> getProvider() -> getModel();
    }

I've changed it to:

protected function getUserClass()
    {
        if(class_exists('App\\User')) {
            return \App\User::class;
        }

        return config('auth.providers.users.model');
    }

But when aa user has a User class which is not the default User model, it will conflict. So I've changed it to: (so it always takes from your auth.php)

protected function getUserClass()
    {
        return config('auth.providers.users.model');
    }

As far as I know (Laravel > 5.*), default User class is in the namespace App, otherwise its configured in your auth.php. So what I've did is a check on class App\\User, and otherwise it takes the class from your auth.php. But this was not a good solution since a developer can also have a User model in their App namespace which is not their default User model, so I've changed it to take it from your config file called auth.php

I think this is a better way to get the Auth Provider class. I'll make a PR in a bit.

Looking forward to your reply!

Cheers,

Tayfun Kayahan

Question about avoiding load three time relation with user model

Hi, if i would to load editor, creator and destroyer relationship without n+1 queries, how can implementing it?
Beacause if i load the three entities relationship

->with(['creator','editor','destroyer'])

This execute three query on user model.

How can i load in one single with?

Laravel 5.3.24

When I try and delete a record I get error ....

Next Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'deleted_by' in 'field list' .....

My Model does not have softdeletes enabled

I ran the migration to create the created_by and updated_by fields .....

updated_by required

When I try create/update a record from a scheduled task, the auth id is not available ... Can you please add something like ... Auth()->id ? Auth()->id : env('WS_BGTASK') instead of Auth()->id ..... Then a user can be created for scheduled tasks and its ID updated to the .env variable ... This way I will have update/create by fields accurate

Laravel 9

Please can you add support for Laravel 9?

Waiting for 1.0.0

Hello @mattmcdonald-uk
Thank you for such a great extension!

What needs to be done to release version 1.0?
Do you need any help?

I would like to use a stable version of in my production project.

deleted_by not set when deleting relationship elements

Hello,

I have just executed this code :

$compte->votes()->delete();
$compte->propositions()->delete();
$compte->delete();

When I go to my database, my user_id is set in the table comptes, in deleted_by.
But anything is set in the tables propositions and votes in the deleted_by field, event if – of course – my lines are well deleted...

Is it a normal behavior ?
Did I make something wrong ?

Thanks for your answer !
Bastien

Naming request

First of, simple but great library! I normally implement this myself manually on observers but this is much slicker.

Do you think editor should be renamed to updater? I just think naming consistency would really polish this repo off.

Change from PHP 5.6 to PHP 5.5.9

Hello, thank you very much for putting together this package.

Our company is still stuck with PHP 5.5 in our production environments. When I found your package, I could not install it because composer.json dictates PHP 5.6 at minimum.

After inspecting the code I found no reason it wouldn't work on PHP 5.5. I downloaded the zip, added an entry to the autoloader and tested it throughly. It works perfectly.

Since it's so simple, I see no drawback in downgrading the requirements to PHP 5.5. Could you make the minimum requirement to be PHP 5.5.9? (same as Laravel 5.1 LTS)

SoftDeletes not work if SoftDeletes is used in a parent class

For example:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Watson\Validating\ValidatingModel;

class Base extends ValidatingModel
{
    use SoftDeletes;
}
<?php

namespace App\Models;

use Wildside\Userstamps\Userstamps;
use App\Models\User;

class Team extends Base
{
    use Userstamps;
}

If you delete Team, the deleted_by field will not be filled with current user.

This is because the return value of the PHP function class_uses does not include any traits used by a parent class. See: http://php.net/manual/en/function.class-uses.php

Support for Laravel 11

It would be awesome if composer.json could be updated to support Laravel 11 that got released yesterday. 🙂
At the moment, the package prevents updating to 11.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.