New model with relations

Let us say I create a table User whose key is userid. There is another table called Project which has a foreign key userid which references User table.

Is it possible to create a new Model that includes both the tables or is a model always associated with a single table.


Yes, it is possible to create new Models and relate different tables as long as you specify all of them in your relations() function per model class.

So User will have a Project and also a Profile if Project and Profile are related to User and User relates to them in their relations() function respectively.

Let us say you want to create a new project.

Can you declare it like

$user = new User() in the controller and pass it to a form that lets you fill up the project details and save the record to a Project table. Here User is a model and project is declared as a relation to the user.


If a project already relates to a user I would just pass the id of the user the project needs to be related to, and then create a hidden field that will hold its user id with a name like Project[userId] (if that is the case) and then when user submits the Project form, it can just update its attributes as normally.

if you check the HTML code of the any form you will figure it out easily. You will see that field names are set in a way of ModelName[attribute]

It is just another way of doing it… without breaking your head against the wall.