party-model

Have you ever been building any app that handled client’s data, was it e-commerce, crm or anything similar, you surely wondered, how to store it in the database.

Most of the time we consider client as Organization or Person, one or the other. Obviously the two are completely different entities in real life, so it wouldn’t be wise to treat them the same. However, speaking of business, we want to treat both entities like a client, no matter if it’s a company or an individual.

So the problem arises, how to design the database efficiently while not having to do checks here and there within our code, to pick what entity we are dealing with this time.

And here comes the Party Model 

– if not familiar with, feel free to google it, there are many discussions already.

 

For this tutorial I’m going to introduce party model as simple as possible, by the Eloquent models and tables for simple e-commerce site.

Let’s start with the Person, that is every invidual, who can become our client:

migration:

 

Now the organization model:

 

 

You may wonder where all the important data is, right? That’s because now we will introduce Party model where we store all the fields that are common to both Person and Organization.

Again, for sake of this tutorial I will keep the things simple, so let’s agree that we store single address, email and phone per client.
What that mean is, that I won’t create separate tables for the above.

Now,  party will be a wrapper for both Person and Orgnization. We will not access these models directly, instead we will call Party model.

With Eloquent this is going to be trivial task. I will setup polymorphic relationship between Party and Organization / Person models. In brief I’m telling Eloquent that Organization and Person is able to be ‘cast’ to Party, thus both will be partyable.

 

So Here’s the Party model and table:

 

 

Now we only need to update Organization and Person models with the relation:

 

And this is it, we have setup relations and prepared the ground for real work.

In the next part we will be loading and storing the data through our Party Model, and I’ll show you how easy it is again with Eloquent!

It's only fair to share...Tweet about this on TwitterShare on RedditPin on PinterestShare on FacebookShare on Google+

Related Post

Tweaking Eloquent relations – how to get lat... Have you ever needed to show only single related model from the hasMany relationship on a set of parents? Being it latest, highest or just rand...
Querying relations in Laravel: get Models where la... Another part of Querying relations in Laravel will cover such problem: I want to get SomeModels that have latest RelatedModel (hasMany) matchin...
Laravel – querying any level far relations w... Eloquent provides one Relation type for far related tables - hasManyThrough. However it works only with with cascade of hasOne/hasMany relations a...
Laravel – how to define and use Eloquent Glo... Laravel 4.2 introduced a new way of handling soft deletes by making use of traits and query builder macros wrapped in as a feature, that many people a...