0 Best Reply Awards

  • Member Since 1 Year Ago
  • 0 Lessons Completed
  • 0 Favorites

4th April, 2017

mix359 left a reply on Order By Eager Loaded Colum • 1 year ago

Thanks for the answers :)

I've done some testing with the various possibilities, and currently the method eager loading + collection sorting seam to take more time to load then the simple join (just with a small set of data).

So I'm trying a different approach (if someone have a similar case):

I've created a TeacherAccount model that extend the Teacher model, and work like the Teacher model, except that it's mean to have even the account data as attributes. So I know that when I'm using that particular model, I've both the teacher and account attribute in it. Here's an example of the model:

class TeacherAccount extends Teacher {

    protected $table = "teachers";

    public function getForeignKey()
        return 'teacher_'.$this->primaryKey;

    public function newQuery($excludeDeleted = true) {
        return parent::newQuery()->join('accounts', 'teachers.id', '=', 'accounts.id');

What do you think of that solution? Any suggestions?

Thanks to all


3rd April, 2017

mix359 started a new conversation Order By Eager Loaded Colum • 1 year ago

Hi to all,

I'm having some trouble finding the right/best way to do one thing:

I've some case where I need to do the sorting based on a column that is loaded through eager loading. Here an example:

Account (id, name, surname) <-> Teacher (account_id)


Usually the eager loading work great in those situation, where I need to access the the surname or name data. But in this situation where I need to order the data using that column, it doesn't work.

I've searched on the other topics and I haven't found a way using only the eager loading. I've found some suggestion about joining the accounts table, something like that Teacher::with("account")->join('accounts', 'teachers.id', '=', 'accounts.id')->orderBy('surname', 'DESC')

That work for the sorting, but have two downside:

  • The data from accounts are loaded 2 times: first time from the joined query, and second time by the eager loading. Also, the data from the first query (the one with the join) is inserted into the Teacher model.
  • I need to know the real name of a table out of the Model (usually in the controller)

There's any way/trick to do the eager loading/with but loading the data through the join? Or any other way to do sorting in those situation of eager loading? Any other suggestion?

Thanks to all byez

20th March, 2017

mix359 left a reply on Roles & Permissions • 1 year ago

Uhm, I haven't used entrust, but it seam similar to another system I'm using. Suddenly many of this system doesn't have any caching ability (from what I know, laravel doesn't use the caching system too much... it simply offer the utility to use it, but it doesn't integrate it with something for example the query result caching... so the packet written for laravel rarely use the cache)

If you don't want to change your permission system, I suggest to try to overwrite the model it's using (in my system I can extend them and pass the new Model through the configuration of the system) Or maybe you can do the same thing with the event system. In this way you can intercept the data queried to those model and cache them. But you also have to delete those data when you change something (like a role or a user-role association).

Hope that can help ;)

Cheers Daniele

mix359 left a reply on Multi User Type Model And Auth • 1 year ago

Thanks for the various answer :) I'm still thinking which solution to use, but some other point of view is interesting ;)

I'm trying to find a way to compare the various solution... maybe I can try to calculate the type and number of query that Eloquent should be do in the various situation... (If it's not too hard/long to do xD) There are some way to predict the result of a test like that? (without creating all the 3 structure, populate with random data and see the query number/time?)

To answer to @Snapey, yes they are all user of my application, watching from the authentication/permission point of view. Looking from other point, it could be something totally different.

For example, if I need to select the users of a classroom, the teacher have particular relation: the teacher-subject-classroom relation. The student have a direct relation with the classroom. The parent should be related to their child and then their child to the classroom.

For the 3 different types of user I have 3 different way to relate to other model. And in many part of my application I need to see they as Teacher, or Student, not as User.

I don't have only simple attributes or profile data to attach to the user. I have relation (and function) and it's different from user type to user type. So I need to have a Model for every type of user, where I can create relation and function that it's unique to that type of user (and the relation can be reversed in other Model, calling for example only the students).

Thanks again Cheers Daniele

mix359 started a new conversation Multi User Type Model And Auth • 1 year ago

Hi to all,

I've a strange situation, that initially seam simply, but it's really complicated to approach. I've to create a system that it's used in a school, so that have 3 different types of users:

  • Student
  • Parent
  • Teacher/Personal

My initial idea was to use 3 different Models/tables, one for user type. In this way is more simple to write many of the relation and controller code. And for the moment this approach worked, because I was only having the teacher/personal to login into the system. But now I have to permit the login of the students and the parents, that should see different thing, etc

To do that I've see to way, but I don't know which is the best/clearest:

  • First way is to use a common Account model, where all the users is, and a flag on the table that represent the user type. Then I will have the other 3 Model (Teacher, Student, Parent) extend the Account model, pointing to the accounts table, and having something like that: public function newQuery() { return parent::newQuery()->where('type', Account::ACCOUNT_TYPE_TEACHER); }

So every generic query that I made on the model, is filtered as a teacher.

I've used a similar approach in the past, and have worked well, but the situations was simpler that this. In this way I've probably a better situation for the authentication and permission management, because I have the same basic Account for all the type of users, but this should probably create some more complex query. Also I have to do other table that contain the data that is not shared from the various types of users: for example, a student have some information about his curriculum. And the difficult part is that I need some intermediary join table to some other table, for example a student will have a classroomId, and I need to have an intermediate table with studentId - classroomId. Or even more complex, a parent have a relation with the student, that is also in the same table.

  • Second way, have the three different Model and table for every user type. The pro of this solution is that I can have the "extra" field (that are not shared from the various user types) directly on the table, so I will have simpler relations and join.

The cons is that I have to use a multi-auth system, and I have to keep track in all the code and all the other Model of the user type. For example, if I need to save on an entity the user that created it, I have to save the user type, and the id of that user in his table (that change from type to type) That make also more difficult to manage the authorization/permission in the code, because I have to check the user type to select the correct user model, and then I can use the user id. So I need to modify or use a different auth system.

Thanks to all that can help me make this decision I wish that this post is clear enough to understand what I'm trying to do :) Cheers Daniele

Edit Your Profile

Want to change your profile photo? We pull from gravatar.com.