Support the ongoing development of Laravel.io →
Database Architecture
Last updated 1 year ago.
0

Generally that's how I do it.

Place a one to many relation on the user model to the notes model.

In the user model:

/**
 *
 * @return Cta
 */
public function notes()
{
    return $this->hasMany('Models\Notes', 'user_id', 'id' );
}

Then add the user_id to the notes table. When a new note is created, you would make sure to attach that note to that specific user.

And when loading a note for a user, you'd want to make sure that user owns that note. So perhaps:

$user->notes()->whereId($note_id)->get()
0

Not sure if you're referring to "accounts" as in users or companies, but if you are referring to companies I have a multitenancy course available on Tuts+ which may help you out.

http://code.tutsplus.com/courses/building-multitenant-apps-in-...

0

Usually with a user_id field too. As machuga mentioned, in multitenancy you may want to approach things differently - a common solution is database per tenant (in fact if working with financial institutions they will probably insist on it) as it should make it more difficult for data to be exposed to another tenant if you forget to do "WHERE tenant = 34" etc

0

@ elite123 For the "one database per tenant" approach, is there a easy way to implent it with Laravel? I guess, I should create a new database on user signup and then have some kind of master database to link accounts to databases.

@machuga Great, I'll take a look!

@phazei Thank you

0

Yes, when you create a tenant you would create the database and tables for that tenant, with a master database to link tenants (subdomain?) to the database. I then have two database connections, one for the master and one for the tenant, I overwrite the tenant connection details when the tenant is detected.

AFAIK there isn't a built in way to do this in laravel and it is probably only worth doing if your tenants demand it or the project is large

0

elite123 said:

Yes, when you create a tenant you would create the database and tables for that tenant, with a master database to link tenants (subdomain?) to the database. I then have two database connections, one for the master and one for the tenant, I overwrite the tenant connection details when the tenant is detected.

AFAIK there isn't a built in way to do this in laravel and it is probably only worth doing if your tenants demand it or the project is large

Ok! Then will all of this information, I will probably use only one database. Thank you for all the answers

0

Sign in to participate in this thread!

Eventy

Your banner here too?

luciendub luciendub Joined 29 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.