03.12.2015 Views

laravel4cookbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Access Control List 57<br />

.<br />

This file should be saved as app/models/Resource.php.<br />

The Resource model is similar to those we’ve seen before; but it also specifies a many-to-many<br />

relationship (in the groups() method). This will allows us to return related groups with $this-<br />

>groups. We’ll use that later!<br />

.<br />

The withTimestamps() method will tell Eloquent to update the timestamps of related groups when<br />

resources are updated. You can find out more about it at: http://laravel.com/docs/eloquent#workingwith-pivot-tables<br />

We also need to add the reverse relationship to the Group model:<br />

1 public function resources()<br />

2 {<br />

3 return $this->belongsToMany("Resource")->withTimestamps();<br />

4 }<br />

.<br />

This was extracted from app/models/Group.php.<br />

.<br />

There really is a lot to relationships in Eloquent; more than we have time to cover now. I will be<br />

going into more detail about these relationships in future tutorials; exploring the different types<br />

and configuration options. For now, this is all we need to complete this chapter.<br />

We can also define relationships for users and groups, as in the following examples:<br />

1 public function users()<br />

2 {<br />

3 return $this->belongsToMany("User")->withTimestamps();<br />

4 }

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!