October: Extending user plugin and define new relationships

Created on 1 Feb 2017  路  4Comments  路  Source: octobercms/october

I decided to use October CMS for back-end of my application. I need to add relationships between user Plugin and my models. so I created UserPlus plugin and tried to add my custom relationships with user Model in boot function of UserPlus plugin. after that, I expected to call my relationships like normal relationships via generated function by October, but actually, I can access my relationship only with direct accessing from user model.

Examples:
Expected : User::find($id)->playlists();
actual: User:find($id)->playlists;

here is how I define my relationships:

public function boot() { UserModel::extend(function ($model) { $model->belongsToMany['playlists'] = [ 'at\PlayList\Models\PlayList', 'table' => 'at_playlist_user_rel_playlist', 'key' => 'user_id' ]; }

I can't find anything related to calling custom relationships in October documents for extended plugins. so, I don't know it's an actual issue or not.

Question

All 4 comments

Calling User::find($id)->playlists() isn't going to return your relationship object, it's going to return your relationship query. Calling a relationship without parenthesis (i.e. an attribute) executes the query for you and returns the results of User::find($id)->relationship()->get().

@LukeTowers Yes, Thank You, that was a mistake from tiredness of night. I didn't receive anything because I didn't call get() method at the end.. but just in case that developers of October need:
when I call normal relationships and make them JSON with default PHP function. they will convert to JSON easily, but that not happen on custom relationships of extended plugin.

at the end: all the problems are gone by calling get() function.

@shkhaksar So can this be closed now?

@LukeTowers yes, thank you for you guides.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mittultechnobrave picture mittultechnobrave  路  3Comments

m49n picture m49n  路  3Comments

SeekAndPwn picture SeekAndPwn  路  3Comments

axomat picture axomat  路  3Comments

oppin picture oppin  路  3Comments