This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sometimes we need to extend out model, with more than data that is fetched from database, for example. | |
But on the other hand, we don't want to do that manually, and we want our framework to do that instead of us. | |
Here's an example, how to do that using Laravel 4. | |
Assume we have model Task, that has some fillable columns. Beside those, predefined columns, we want to attach additional data, let's say, parent task (and all its data). | |
First, we will define relationship between 'task' and 'task parent'. It is one-to-many relationship. We'll do it based on column named 'parent_id'. | |
public function parentTask(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assume we have a following situation: | |
- We have a task that has associated checklists (one or many). | |
- Every checklist has associated items (again, one or many). | |
In Laravel 4, we can use Eager Loading to fetch all associated data. | |
Let's take a look. | |
First, we have have a TaskChecklistItem model as following: |
NewerOlder