/** * Hydrate the Pivot model on an array of results. * * @param array $results * @return void */ protected function hydrate_pivot(&$results) { foreach ($results as &$result) { // Every model result for a many-to-many relationship needs a Pivot instance // to represent the pivot table's columns. Sometimes extra columns are on // the pivot table that may need to be accessed by the developer. $pivot = new Pivot($this->joining, $this->model->connection()); // If the attribute key starts with "pivot_", we know this is a column on // the pivot table, so we will move it to the Pivot model and purge it // from the model since it actually belongs to the pivot model. foreach ($result->attributes as $key => $value) { if (starts_with($key, 'pivot_')) { $pivot->{substr($key, 6)} = $value; $result->purge($key); } } // Once we have completed hydrating the pivot model instance, we'll set // it on the result model's relationships array so the developer can // quickly and easily access any pivot table information. $result->relationships['pivot'] = $pivot; $pivot->sync() and $result->sync(); } }
protected function hydrate_pivot(&$results) { foreach ($results as &$result) { $pivot = new Pivot($this->joining, $this->model->connection()); foreach ($result->attributes as $key => $value) { if (starts_with($key, 'pivot_')) { $pivot->{substr($key, 6)} = $value; $result->purge($key); } } $result->relationships['pivot'] = $pivot; $pivot->sync() and $result->sync(); } }