Hey guys,
So I've hit a bit of a roadblock. So I have three tables of interest:
Table(simplified): Tickets
- id (pk)
- user_id (fk)
- address_id (fk) (nullable)
Table(simplified): Users
Table(simplified): Address
- id(pk)
- type (0 for residence, 1 for ticket)
- address
So Users upon sign up have to list their residence which gets stored in the meta_address table. When a user submits a ticket, they have the option to:
- Associate their Ticket with their residence address.
- Associate their Ticket with a different address / location
They also don't have to list an address associated with the Ticket, meaning the address_id field in Table::Tickets could be empty if need be.
So to recap with this logic there are 3 states:
STATE = NULL in the scenario where the ticket is regarding something that is not tied to a physical location.
STATE = User Residence in the scenario where the ticket is regarding the users home address.
STATE = Misc Location in the scenario where the ticket is any other type of address or coordinate.
Say meta_address table has a single row:
id: 13
type: residence
address: Foobar Road
During Ticket creation, when the User states that the Ticket is the same as their residence. I want the address_id in the Table:Tickets to be listed as '13'. I don't want it to create a new row.
If during Ticket creation, the user states a new address. I'd want a new row to be added to meta_address like this:
id: 14
type: ticket
address: SegFault Ave
Now I'm not sure which route would be the best to do this. Would the morphTo() route be the best option here? Like this:
class Address {
public function addressable(){
return $this->morphTo();
}
}
class User {
public function address(){
return $this->morphTo('App\Address','addressable');
}
}
class Ticket {
public function address(){
return $this->morphTo('App\Ticket','addressable');
}
}
Anyone have any thoughts on this? Would this be the right way of doing this?