What's the best Laravel Eloquent way to structure this - laravel-5

If the question is not StackOverflow-y I don't mind moderators to close it, I couldn't decide.
I have the following models: Users, Groups, Regions, Cities... There are like 4 of them. Every user can have multiple relations to each entity (administrator, manager, participant and some others)
Should I do a Relations table as follows:
user_id | target_id | target_type | relationship_type
1 | 2 | group | manager
And if so - how would I use Eloquent relations - HasManyThrough somehow?
Or is the Eloquent way something else? Maybe different tables for each entity (user_group, user_region) ?
The goal is to have each "Groups where Peter is admin in", "Regions where Jessica is participant" queried easily via Eloquent.

Related

a model with several one-to-many relationships

I'm using gorm to represents a model and do several operations.
The model contains an entity which has a one-to-many relationship with another entity an this has a one-to-many relatioship with another entity too.
Something link this:
+---------+ +------------+ +------------+
| entity1 | one-to-many | entity 2 | one-to-many | entyty3 |
| |------------->| entity1ID |------------->| entity2ID |
+---------+ +------------+ +------------+
When I run a complete update operation of the model the final state of postgres is inconsistent.
I haven't found info about gorm samples involving complex models. Most samples only contains one entity and a few samples contains models with two entities.
I'm starting to think that gorm isn't for complex projects and models.
How can I do an update operation with a consistent result in the storage?

Sqlboiler define relations

I have a sqlite database for which I need read only access in go. I have been exploring sqlboiler as an ORM and it's been great so far generating the models etc but 1 thing I was not able to figure out is how to define custom relations. I know that it does this automatically when the db has foreign keys etc but my db doesn't and I can't change its structure (it's written by another application). So I have a structure like this:
books:
id | title
1 | Sample title
authors:
id | name
1 | Author Name
book_authors:
book_id | author_id
1 | 1
I think it's fairly obvious what I want to do, this is a many to many relation between books and authors. The problem is that book_id and author_id are not foreign keys. Is there any way I can configure the code generation inside the toml file to create this relation or will I have to write code manually to do that? Thanks

Many to many Laravel Eloquent relationship with multiple intermediate tables

I'm using Laravel 5.4 and have a model and table structure as follows:
users
-----
id
accounts
--------
id
holdings
--------
id
account_id (foreign key to account)
user_accounts
-------------
id
user_id
account_id
A user can have multiple accounts
An account can be shared by multiple users
Each account has multiple holdings
A user therefore indirectly has many holdings through their many accounts.
I need help to define a relation on the User model called "holdings" to get me all the holdings applicable to the user (based on the accounts they are linked to).
I've tried lots of different things and spent ages on google. I can get close with both belongsToMany and hasManyThrough, but they only seem to work for 3 table relationships where the intermediate table stores primary keys from the other tables. I can reduce my relationship to 3 tables (rather than 4) if I make use of the account_id foreign key on the holdings table to remove the need to join through the accounts table, however I can't seem to get this to work.
belongsToMany - holdings.id needs to be holdings.account_id:
select * from `holdings`
inner join `user_accounts` on `holdings`.`id` = `user_accounts`.`account_id`
where `user_accounts`.`user_id` = ?
hasManyThrough - user_accounts.id needs to be user_accounts.account_id:
select * from `holdings`
inner join `user_accounts` on `user_accounts`.`id` = `holdings`.`account_id`
where `user_accounts`.`user_id` = ?"
Well it's not strictly an answer as I was asking about laravel 5.4, but it is a possible solution.
It turns out I'm in luck as I came across this Laravel belongsToMany relationship defining local key on both tables which indicates the belongsToMany relationship has been enhanced in Laravel 5.5 to support this type of scenario.
I have upgraded my project to L5.5 and replaced my hack with the relation:
return $this->belongsToMany(Holding::class, 'user_accounts', 'user_id', 'account_id', null, 'account_id');
And am happy to say it seems to work perfectly - at least for basic gets which is my use case, I haven't tried any updates etc.
Thanks to #cyberfly and those who posted the answer above

JPA, Is it possible to map one class to multiple tables with the same structure but different names?

I need to record every day's information, like the food price, so I created almost infinite tables named with the same pattern as food_${date}, like food_20180222, food_20180223, and etc.
Each table has the same structure as follows
+----+-------+--+
| Id | Price | |
+----+-------+--+
| 1 | 10 | |
+----+-------+--+
I wonder that by using Spring JPA, is it possible to map one food entity to those tables with different names? Or can food entity be dynamically mapped to one of the tables determined by date?

laravel relationships - 3 way pivot - eloquent

I am following a tutorial on Laravel ACL, that takes into consideration Users and Roles (https://github.com/laracasts/laravel-5-roles-and-permissions-demo). However, I have an additional level of complexity whereby I need to take into consideration a company model also. So a user may belong to many companies and each instance of a company user may have many roles.
I think the best way to acheive this would use a pivot that has 3 fields:
company_id
role_id
user_id
Esentially, I want to achive something like this:
$user->companyRoles; //return the user's company roles
$user->company->assignRole('admin');
$user->assignRole($companyId, 'admin');
Can you advise on the relationships I require to do this?
Many thanks

Resources