i have 2 main models and some child models with relationController to main models, now i need to add user id to both main and child models so user can only access its own data since users data stay on the same table.
i create belongsToMany
relationship from user.php
to the models and vice versa with pivot table
but nothing happen
i have no idea how to do this since i need simplified database management in case i need to do migration..
can someone share your experience on this..

- 137
- 13
-
add your models and the required output – Jun 10 '18 at 19:11
1 Answers
I can suggest you that you just add user_id
[for Backend => backend_users_id ] to models which data you want access to owner only.
now in main model
you can define belongsTo
relation and in user you can define hasMany
relation
main model relationship [if you want to restrict the user in backend side then you need to add backend user relation and same in backend user model]
class MainModel extends Model
{
// Adding relation to user model
public $belongsTo = [
'user' => 'RainLab\User\Models\User',
'backend_users' =>'Backend\Models\User' // for Backend
];
}
adding a relation to the user model [ you need to put this code in your plugin's boot method ]
// Extending User Model
\RainLab\User\Models\User::extend(function($model) {
$model->hasMany['mainmodel'] = ['HardikSatasiya\Plugin\Models\MainModel'];
});
// for Backend users
\Backend\Models\User::extend(function($model) {
$model->hasMany['mainmodel'] = ['HardikSatasiya\Plugin\Models\MainModel'];
});
Now access data [ Front-end side ]
// Returns the signed in user
$user = \Auth::getUser();
dd($user->mainmodel); // it will return collection of related mainmodels
// it will return related data and now its filter by owner
dd($user->mainmodel[0]->otherRelatin);
// for Backend users
// Returns the signed in user
$user = \BackendAuth::getUser();
dd($user->mainmodel); // it will return collection of related mainmodels
// it will return related data and now its filter by owner
dd($user->mainmodel[0]->otherRelatin);
Example filter data in listview based on logged in admin user [ OctoberCMS do not give such functionality out of the box, you can not hide a portion of data records you can hide entire menu or all records based on rights and roles but can not hide partial records ]
public function listExtendQuery($query)
{
$user = \BackendAuth::getUser();
$query->where('backend_users_id', $user->id);
}
to add
backend_users_id
you can use this code
class MainModel extends Model {
public function beforeSave()
{
$user = \BackendAuth::getUser();
$this->backend_users_id = $user->id;
}
}
if any doubt please comment.

- 9,547
- 3
- 22
- 40
-
So i need to install user plugin before i do this.. is it correct? – Isral Bustami Jun 11 '18 at 06:13
-
I am building backend administration for mobile app.. so all of this for backend side.. – Isral Bustami Jun 11 '18 at 06:16
-
Do i need to create pivot table for this or i just need add `user_id` in model database – Isral Bustami Jun 11 '18 at 06:18
-
1if you are using it in backend then you need to use backend users table and it is already available where you can use it `Backend\Models\User` and updated answer please check it – Hardik Satasiya Jun 11 '18 at 09:32
-
`dd($user->mainmodel)` where do i put this? controller or boot function? – Isral Bustami Jun 11 '18 at 09:40
-
1it's just to view the dump of the `model` you don't need to put it anywhere, its for debugging purpose – Hardik Satasiya Jun 11 '18 at 10:05
-
Sir.. i did your suggestion exactly the way you suggest me to do.. but nothing happen.. i try to create new data from each user account.. still showing all data... and i check in database `backend_users_id` is not defined which is `0` – Isral Bustami Jun 12 '18 at 02:40
-
1showing all data you need to implement it as october is not come with that functionality, as well when model is saved in db you need to attached be user to model manually – Hardik Satasiya Jun 12 '18 at 04:19
-
1i am afraid you were thinking all will be built in october but its not we need to add filters and data manually. though you can add restriction to portion of backend using default functionality but can not put restriction on data .. using default one. – Hardik Satasiya Jun 12 '18 at 04:21
-
Ok sir.. i believe the purpose of suggestion method is to filter data to its owner.. but i didnt happen in my side.. – Isral Bustami Jun 12 '18 at 09:05
-
1the suggested method is to add BE user data to record not to filter data. means in the controller you need to manually add filters - updated answer to filter data but its will filter only in list view. you need to add manually all the things this answer can not cover all the filter criteria so added example – Hardik Satasiya Jun 12 '18 at 09:14
-
Thank you sir.. i am newbie so forgive me for the question that i made.. i have the logic but i dont know how to turn it to code.. – Isral Bustami Jun 13 '18 at 04:39
-
I read the documentation.. will scope do the work for output that i want.. – Isral Bustami Jun 13 '18 at 04:40
-
1yes, the scope can work and to add a backend user id updated question you can check it. – Hardik Satasiya Jun 13 '18 at 09:14
-
Thank you sir.. it works.. but if change id in the browser on update.. BE user can access all data.. i try to use formExtendQuery but it doesnt work.. – Isral Bustami Jun 16 '18 at 09:18
-
what id you are changing and can you share your current filter logic means currently how you are filtering data if I can help there? – Hardik Satasiya Jun 16 '18 at 14:46
-
Id in the url record on update page.. i use the query that suggest and add this method https://github.com/octobercms/october/issues/1780#issuecomment-186411450 – Isral Bustami Jun 16 '18 at 15:40
-
-
1its done.. i use formExtendedQuery.. and its secure now.. user can not access other user data.. thank you so very much @HardikSatasiya – Isral Bustami Jun 20 '18 at 05:33
-
1cool, sorry i was busy lately so couldn't follow you up before, but glad its finally done :) – Hardik Satasiya Jun 20 '18 at 08:06