ModelExpansion.php 689 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/6/4
  6. * Time: 下午7:06
  7. */
  8. namespace app\common\models;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Database\Eloquent\Relations\Relation;
  11. class ModelExpansion
  12. {
  13. public function getRelationshipFromExpansion($method,Model $model){
  14. $relations = $this->$method($model);
  15. if (! $relations instanceof Relation) {
  16. throw new \LogicException('Relationship method must return an object of type '
  17. .'Illuminate\Database\Eloquent\Relations\Relation');
  18. }
  19. $model->setRelation($method, $results = $relations->getResults());
  20. return $results;
  21. }
  22. }