last day (15 days later) » 

10:58 PM
0
Q: Laravel - Correct Model Relationship for pivot table?

WonkaI can't figure out the best way to setup the relationships for this simple situation, in a way which makes inserts/querying, in one go with everything related being inputted/queried at once using eloquent. Looks like a pivot table should do it if it was just owner_renter, but here I have owner_re...

 
I suppose there can't be more than one owner for any room, correct? What about renter?
 
@lukasgeiter I am trying to have the same table structure with all 3 fields, it's a pretty simple structure I had outside Laravel, but when it comes to defining it as a relationship for all 3, it was a bit confusing on how to do it correctly.
@JarekTkaczyk I added an update and a table screenshot that may help. Each owner has many rooms, and there is only one owner for each room. There can be many renters for a room (roommates).
 
@Wonka That's what I thought. Then you definitely don't need that pivot table. Add foreign key owner_id to the rooms table. Next, if a renter may rent more than one room, then leave the pivot table linking renters and rooms. Otherwise add foreign key room_id to the persons(renters) table, and then it's good.
 
@JarekTkaczyk That would work except with a room_id of 0 situation. I am trying to not explicitly state every room in a 0 situation, which would mean all rooms. So in a community, if the owner builds a game room or library room, the 0 would automatically handle that, which would mean the 'owner_id' would have to be in the same table as a reference to which person (owner_id) it belongs to and it will become part of all person(renter_id). I do agree though that probably a pivot may not be suited for this situation, but can't seem to figure this one out with a different table structure.
 
Elaborate please, I don't know what you mean and what the problem is.
 
11:10 PM
hit me
 
Okay, so a person can be an owner or renter. An owner can have many rooms for rent, like a bedroom, attic room, (and can build future rooms, like a game room, library room). A renter can either rent a specific room, like a bedroom (with a room_id of 1), or an attic room (with a room_id of 2), these will be added to the 3 field table. There is also an option to rent all rooms of owner now or future (so if a new person clicks on it), they get subscribed to room_id of 0
which means that they will automatically be subscribed to the owners future rooms
I think it's easier to understand this way maybe it's confusing with the rooms
hmm
I don't know how to explain it
 
first things first
renter can have many rooms
and many renters can have the same room?
 
okay, say a site like stack overflow, this same logic should still apply but may be a lot clearer
say you have your questions
and I have my questions
I can subscribe to all your questions
or just one question
 
yeah I get it
so you link User-Question
OR User-User
 
yeah exactly
so I am not sure if polymorphic should work for this
 
11:18 PM
no
 
because I don't know if pivot will work
 
back to the question - renter can have many rooms?
and many renters can have the same room?
 
yes - it's the same model as the questions
or should be
 
so:
Room belongsTo User (owner_id)
Room belongsToMany User (pivot table, renters)
User belongsToMany Room (pivot table, renters)
User hasMany Room (owns the rooms)
User belongsToMany User (all the rooms, future etc)
I don't see any usage for that pivot table you started with
all those relations are pretty much separate
 
yeah - I am a bit confused now
can we stick with the stackoverflow example
seems simpler to understand
questions/user
user has many questions
user can subscribe to many questions
user can subscribe to one question of any user
 
11:26 PM
sure, User asks many Questions (hasMany / belongsTo)
User subscribes to many Questions (belongsToMany with pivot)
User subscribes to another User (belongsToMany with pivot) in order to see all his Questions
 
yeah - that sounds right
so it should be 2 pivots then it seems
 
2 pivots, each doing its job only
 
hmm..
I think I was complicating it with one table probably
because if it was 0 it should be subscribe to all questions
I thought maybe it's better to have it in a single table
 
U don't think so and I even couldn't think of any Eloquent representation for this
anyway, try to model things like they are in real life
those are separate entities (User, Question or whatever) so subscribing to User doesn't mean subscribing to all Questions
 
I think I'll try that out Jarek
 
11:33 PM
of course it will lead to show all the questions of this user in the end, but that's logic part
 
if you don't mind
 
have fun
 
I checked out your site a few times
when searching for different things
and it's very advanced and great
I am just wondering how/where you learn all this stuff
lol
 
thx, I have bunch of different drafts, but no time to work on it :/
just playing around with Laravel and Eloquent
for Eloquent the best way is artisan tinker and playing in REPL
realtime is waaay better than through browser, controllers and stuff
so make sure you give it a try
 
I never heard of REPL
 
11:35 PM
check it out, it's command line tool
good luck
now I'm off
 
thank you very much
I can accept your answer
if you put a simple one to use 2 pivots
thanks a lot for your help
 

last day (15 days later) »