Adding additional user data on User Module



  • Hey,

    I am trying to add additional fields for user module based on your

    .

    How you guys are storing data to profile__profiles table ? Event is not firing in my case

    <?php namespace Modules\Profile\Events\Handlers;

    use Modules\Profile\Repositories\ProfileRepository;
    use Modules\User\Events\UserHasRegistered;

    class StoreProfileData {

    /**
     * @var ProfileRepository
     */
    private $profileRepository;
    
    /**
     * @param ProfileRepository $profileRepository
     */
    public function __construct(ProfileRepository $profileRepository)
     {
         parent::__construct();
    
         $this->profileRepository = $profileRepository;
     }
    
    /**
     * @param UserHasRegister and UserWasUpdated $event
     */
    
    public function handle($event)
    {
        dd($event);
    
        if(get_class($event) === UserHasRegistered::class)
        {
            $data = $event->data;
    
            $data['user_id'] = $event->user->id;
    
            return $this->profileRepository->create($data);
        }
    
    }
    

    }


  • Global Moderator

    @deben You have your provider setup correctly?



  • // This is my EventServiceProvider.php

    <?php namespace Modules\Profile\Providers;

    use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
    use Modules\Profile\Events\Handlers\StoreProfileData;
    use Modules\User\Events\UserHasRegistered;
    use Modules\User\Events\UserWasUpdated;

    class EventServiceProvider extends ServiceProvider{

    protected $listen = [
    
        UserHasRegistered::class => [
    
            StoreProfileData::class,
    
        ],
    
        UserWasUpdated::class => [
    
            StoreProfileData::class,
    
        ]
    
    ];
    

    }



  • @armababy

    In the documentation here not specified , how user_id stores on profile__profiles table ? How UserHasRegistered and UserWasUpdated events firing?

    Do your needful help on this.


  • Global Moderator

    @deben

    Okay,

    So for a event handling to work in the way you are describing you need following things in order:

    1. Create event handler anywhere you want (typically in Modules/Profile/Events/Handlers).
    2. Create EventServiceProvider extend it with Illuminate\Foundation\Support\Providers\EventServiceProvider fill protected $listen array with event to handler.
    3. Register your EventServiceProvider in modules.json providers section.

    As it seems to me you have covered 1 and 2 of 3 steps, you would need to add your service provider to modules.json to get this thing going.

    So add "Modules\\Profile\\Providers\\EventServiceProvider" at the end of providers array in that modules.json of your profile module



  • @armababy

    I had already added . but this was not working.
    //This is my module.json file looks like

    {
    "name": "Profile",
    "alias": "profile",
    "description": "",
    "version": "v0.1",
    "keywords": [],
    "active": 1,
    "order": 1,
    "providers": [
    "Modules\Profile\Providers\ProfileServiceProvider",
    "Modules\Profile\Providers\RouteServiceProvider",
    "Modules\Profile\Providers\EventServiceProvider"
    ],
    "aliases":{},
    "files": [

    ]
    

    }

    // config/relations.php

    <?php

    return [
    'User' => [
    'profile' => function ($self) {
    return $self->belongsTo('Modules\Profile\Entities\Profile', 'id', 'user_id')->first();
    }
    ]
    ];

    // Profile/Entities/profile.php

    <?php namespace Modules\Profile\Entities;

    use Dimsav\Translatable\Translatable;
    use Illuminate\Database\Eloquent\Model;

    class Profile extends Model
    {
    use Translatable;

    protected $table = 'profile__profiles';
    public $translatedAttributes = [];
    protected $fillable = ['user_id','about_user','designation','profile_img'];
    
    public function user()
    {
        $driver = config('asgard.user.users.driver');
    
        return $this->belongsTo("Modules\\User\\Entities\\{$driver}\\User");
    }
    

    }


  • Global Moderator

    @deben

    hey, please read some markdown formating :) user 3x prime symbols to make code block and close with 3x prime symbols.

    I have feeling that you are mixing too much information here.

    Lets deal with events first.
    Entities and configs and relations have nothing to do with events. Have you fixed events?
    I have tested and with 3 files (modules.json, eventserviceprovider.php and handler) you can handle events.
    Also might try php artisan dump-autoload.

    After that we can deal with another issue.



  • @armababy

    Hey,
    Once I run- php artisan dump-autoload

    I got
    [InvalidArgumentException]
    Command "dump-autoload" is not defined.



  • @armababy

    I did try composer dump-autoload but it's not working.


  • Global Moderator

    @deben

    I have no more suggestions, it seems all okay and i tested events are firing your provider just aren't handling them.
    That would suggest cached list of service providers.

    try php artisan clear-compiled or php artisan module:dump profile



  • @armababy

    Hey,
    I tried from scratch installing new asgardcms and create profile module to add extra fields to User Module. But same thing happens ,Event is not working .

    So can you please give me your working Event Handler file and Providers file for Profile Module.


  • Global Moderator

    @deben

    There you go did clean install then this.

    1. php artisan asgard:module:scaffold (bekesh/UserProfile, Elequent, no entities, no value objects)
    2. Make file Modules/UserProfile/Providers/EventServiceProvider.php
    3. Make file Modules/UserProfile/Events/Handlers/EventHandler.php (make folders Events -> Handlers)
    4. Fill Modules/UserProfile/Events/Handlers/EventHandler.php with this
    5. Fill Modules/UserProfile/Providers/EventServiceProvider.php with this
    6. Add "Modules\\UserProfile\\Providers\\EventServiceProvider" to Modules/UserProfile/module.json in providers array like this
    7. Login to backend and edit admin user, change something (name for example) - outputs this


  • @armababy

    Ok Cool, this was working fine for Update users. But when am trying to register new user "UserHasRegistered " event not working. Which event should I used for register new user? Is there any other event ? Can you please try with register new user and check event is working for you or not.


  • Global Moderator



  • @armababy

    Hey,
    I got registered user id using UserHasaRegistered event and stored on profile__profiles table. But why the additional fields like about_user,designation are not storing on profile__profiles table. How can I debug this? In the documentation you have specified input name syntax as profile[your-field-name]. Am I doing anything wrong? Following are the two fields on create user view.(from backend new user registration).

          <div class="form-group{{ $errors->has('profile.designation') ? ' has-error has-feedback' : '' }}">
         {!! Form::label('profile[designation]', trans('profile::profiles.form.designation')) !!}
       <input class="form-control" placeholder="{{ trans('profile::profiles.form.designation') }}"
                                   name="profile[designation]" type="text" id="profile[designation]" value="{{ Input::old('profile.designation') }}">
        {!! $errors->first('<profile class="designation"></profile>', '<span class="help-block">:message</span>') !!}
        </div>
    
       <div class="form-group{{ $errors->has('profile.about_user') ? ' has-error has-feedback' : '' }}">
    {!! Form::label('profile[about_user]', trans('profile::profiles.form.about user')) !!}
    
    <textarea cols="20" rows="3" class="form-control" placeholder="{{ trans('profile::profiles.form.about user') }}" name="profile[about_user]" type="text" id="profile[about_user]" value="{{ Input::old('profile.about_user') }}"></textarea>
    
    {!! $errors->first('profile.about_user', '<span class="help-block">:message</span>') !!}
      </div>


  • @nWidart @armababy

    Hey,

    I am waiting for your response . I got stuck.


  • Global Moderator

    @deben

    I currently don't have much time to test out your code :(
    I'll post here when and if i do.



  • @armababy

    Did you get a chance to look into my code?



  • hi @deben, you did work this issue can put addional data on profile user ?


Log in to reply