-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix belongto relationship #79
base: master
Are you sure you want to change the base?
fix belongto relationship #79
Conversation
Add a table alias when sorting to the belongsTo relation. This is to prevent errors when the main table and child table have the same table name.
@rizkyseptiawan Two tables cannot have same name, you might have meant self join but I don't think it would be a issue as we use framework methods to access to keys. |
@Lakshan-Madushanka Yes, I mean self join. As I said, one example case is when I have a users table and in that column for example there is a column that does a self join (created_by_id). In the code that I have pulled request, I have included to replace the table obtained from the framework methods to match the alias name |
Then what would be the query string ? |
@rizkyseptiawan I don't think we can do a self join using just a query string and adding table aliases doesn't make sense to me. You have to define a relationship or use query scopes. Users extends Model {
public function scopeFromDifferentCities(Builder $query) {
$query->join('users as u2', 'users.id', '<>', 'u2.id')
->whereColumn('users.city', 'u2.city')
->distict()
}
}
User::fromDifferentCities()->get() If I have mistaken, Actually your title and description is very misleading and subtle and you should use real world scenario like above and example code blocks to explain something complex like this. Finally you should implement test cases to prove what your doing is right and understandable. |
Add a table alias when sorting to the belongsTo, hasOne, and hasMany relation. This is to prevent errors when the main table and child table (sort subquery) have the same table name. For example: the users table has a created_by_id column, where the created_by_id column also refers to the users table.