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); } }
}
-
@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, ] ];
}
-
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.
-
Okay,
So for a event handling to work in the way you are describing you need following things in order:
- Create event handler anywhere you want (typically in Modules/Profile/Events/Handlers).
- Create
EventServiceProvider
extend it withIlluminate\Foundation\Support\Providers\EventServiceProvider
fillprotected $listen
array with event to handler. - Register your
EventServiceProvider
inmodules.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 thatmodules.json
of your profile module
-
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"); }
}
-
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 tryphp artisan dump-autoload
.After that we can deal with another issue.
-
Hey,
Once I run- php artisan dump-autoloadI got
[InvalidArgumentException]
Command "dump-autoload" is not defined.
-
I did try composer dump-autoload but it's not working.
-
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
orphp artisan module:dump profile
-
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.
-
There you go did clean install then this.
php artisan asgard:module:scaffold
(bekesh/UserProfile, Elequent, no entities, no value objects)- Make file
Modules/UserProfile/Providers/EventServiceProvider.php
- Make file
Modules/UserProfile/Events/Handlers/EventHandler.php
(make folders Events -> Handlers) - Fill
Modules/UserProfile/Events/Handlers/EventHandler.php
with this - Fill
Modules/UserProfile/Providers/EventServiceProvider.php
with this - Add
"Modules\\UserProfile\\Providers\\EventServiceProvider"
toModules/UserProfile/module.json
in providers array like this - Login to backend and edit admin user, change something (name for example) - outputs this
-
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.
-
@deben Answered on your other question http://forum.asgardcms.com/topic/82/userhasregistered-event-not-working/2
-
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>
-
-
I currently don't have much time to test out your code
I'll post here when and if i do.
-
Did you get a chance to look into my code?
-
hi @deben, you did work this issue can put addional data on profile user ?