Skip to content

Instantly share code, notes, and snippets.

@ndamnjanovic
ndamnjanovic / accessor_laravel_4
Created October 28, 2013 14:34
Extending Model using Laravel 4
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(){
@ndamnjanovic
ndamnjanovic / eloquent_eager_loading
Last active December 26, 2015 18:49
Laravel 4 - Eager loading
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: