What is "touches"? Well, anyone familiar with Linux/Unix is going to be familiar with the concept of "touching" a file. Touching a file sets the "last modified" timestamp (or creates the file if it doesn't exist). Knowing when a file was last modified can be very important for version control.
Laravel has something similar for its model relations. For example, lets say there is a club with many members. When a member joins, leaves or gets a new role in the club, you might contend that the club has changed. In terms of Laravel, you would want the club table's "updated_at" timestamp to reflect that change. Enter the "touches" property...
To make the magic happen in Laravel, you simply need to define the parent relationship and add it to the touches property like so:
Laravel has something similar for its model relations. For example, lets say there is a club with many members. When a member joins, leaves or gets a new role in the club, you might contend that the club has changed. In terms of Laravel, you would want the club table's "updated_at" timestamp to reflect that change. Enter the "touches" property...
To make the magic happen in Laravel, you simply need to define the parent relationship and add it to the touches property like so:
<?php | |
class Club extends Model | |
{ | |
/** | |
* Touches (touch parent model when this model changes) | |
*/ | |
protected $touches = ['members']; | |
/** | |
* Member relation (many to one) | |
*/ | |
public function members() | |
{ | |
return $this->belongsTo('App\Club'); | |
} | |
} |
That's it! Now every time you make a change to a member record, the club record will reflect that it changed as well.
Comments
Post a Comment