资讯专栏INFORMATION COLUMN

Eloquent: 修改器

james / 2670人阅读

摘要:好了,废话不多说,今天来说一说修改器。前两天看一同事好像对这个修改器了解不多,所以今天就拿它作为入口,扒一扒其实现源代码。前端就可以直接拿到结果同样的,还有方法来定义一个修改器。

感觉好长时间没写东西了,一方面主要是自己的角色发生了变化,每天要面对各种各样的事情和突发事件,不能再有一个完整的长时间让自己静下来写代码,或者写文章。

另一方面现在公司技术栈不再停留在只有 Laravel + VUE 了,我们还有小程序、APP 等开发,所以我关注的东西也就多了。

接下来我还是会继续持续「高产」,把写技术文章当作一个习惯,坚持下去。

好了,废话不多说,今天来说一说「Eloquent: 修改器」。

一直想好好研究下 Eloquent。但苦于 Eloquent 有太多可研究的,无法找到一个切入点。前两天看一同事好像对这个「Eloquent: 修改器」了解不多,所以今天就拿它作为入口,扒一扒其实现源代码。

首先还是拿一个 Demo 为例:

Demo
birthday);
        return Carbon::now()->diffInYears($date);
    }
}

这个代码比较简单,就是通过已有属性 birthday,计算 Baby 几岁了,得到 age 属性。

前端就可以直接拿到结果:

return $baby->age;

同样的,还有 setXxxAttribute 方法来定义一个修改器。

源代码

读代码还是从使用入手,如上通过 $baby->age 调用 age 属性,这个属性没在类中定义,所以只能通过 PHP 的魔术方法 __get() 调用了。

我们看看 Model 类的 __get() 方法:

/**
 * Dynamically retrieve attributes on the model.
 *
 * @param  string  $key
 * @return mixed
 */
public function __get($key)
{
    return $this->getAttribute($key);
}

好了,我们开始解读源代码了:

/**
 * Get an attribute from the model.
 *
 * @param  string  $key
 * @return mixed
 */
public function getAttribute($key)
{
    if (! $key) {
        return;
    }

    // If the attribute exists in the attribute array or has a "get" mutator we will
    // get the attribute"s value. Otherwise, we will proceed as if the developers
    // are asking for a relationship"s value. This covers both types of values.
    if (array_key_exists($key, $this->attributes) ||
        $this->hasGetMutator($key)) {
        return $this->getAttributeValue($key);
    }

    ...
}

重点自然就在第二个 if 上,主要判断 attributes 数组中是否包含该属性,如果没有,则会执行函数 $this->hasGetMutator($key)

/**
 * Determine if a get mutator exists for an attribute.
 *
 * @param  string  $key
 * @return bool
 */
public function hasGetMutator($key)
{
    return method_exists($this, "get".Str::studly($key)."Attribute");
}

这就对上了我们的 Demo 中自定义的函数 getAgeAttribute(),也就返回 true 了。

接下来就是执行函数 $this->getAttributeValue($key),进而执行函数:return $this->mutateAttribute($key, $value);

/**
 * Get the value of an attribute using its mutator.
 *
 * @param  string  $key
 * @param  mixed  $value
 * @return mixed
 */
protected function mutateAttribute($key, $value)
{
    return $this->{"get".Str::studly($key)."Attribute"}($value);
}

好了,到此我们基本就知道了获取自定义 Attribute 的流程了。

相信解析 set XxxAttribute 也是很简单的。

总结

好长时间没写东西了,先从最简单的入手,练练手。解析 Eloquent 需要费很多脑细胞,接下来的一段时间我会围绕着这个主题好好研究下去,尽可能的全部解读一遍::

.
|____Capsule
| |____Manager.php
|____composer.json
|____Concerns
| |____BuildsQueries.php
| |____ManagesTransactions.php
|____Connection.php
|____ConnectionInterface.php
|____ConnectionResolver.php
|____ConnectionResolverInterface.php
|____Connectors
| |____ConnectionFactory.php
| |____Connector.php
| |____ConnectorInterface.php
| |____MySqlConnector.php
| |____PostgresConnector.php
| |____SQLiteConnector.php
| |____SqlServerConnector.php
|____Console
| |____Factories
| | |____FactoryMakeCommand.php
| | |____stubs
| | | |____factory.stub
| |____Migrations
| | |____BaseCommand.php
| | |____FreshCommand.php
| | |____InstallCommand.php
| | |____MigrateCommand.php
| | |____MigrateMakeCommand.php
| | |____RefreshCommand.php
| | |____ResetCommand.php
| | |____RollbackCommand.php
| | |____StatusCommand.php
| |____Seeds
| | |____SeedCommand.php
| | |____SeederMakeCommand.php
| | |____stubs
| | | |____seeder.stub
|____DatabaseManager.php
|____DatabaseServiceProvider.php
|____DetectsDeadlocks.php
|____DetectsLostConnections.php
|____Eloquent
| |____Builder.php
| |____Collection.php
| |____Concerns
| | |____GuardsAttributes.php
| | |____HasAttributes.php
| | |____HasEvents.php
| | |____HasGlobalScopes.php
| | |____HasRelationships.php
| | |____HasTimestamps.php
| | |____HidesAttributes.php
| | |____QueriesRelationships.php
| |____Factory.php
| |____FactoryBuilder.php
| |____JsonEncodingException.php
| |____MassAssignmentException.php
| |____Model.php
| |____ModelNotFoundException.php
| |____QueueEntityResolver.php
| |____RelationNotFoundException.php
| |____Relations
| | |____BelongsTo.php
| | |____BelongsToMany.php
| | |____Concerns
| | | |____InteractsWithPivotTable.php
| | | |____SupportsDefaultModels.php
| | |____HasMany.php
| | |____HasManyThrough.php
| | |____HasOne.php
| | |____HasOneOrMany.php
| | |____MorphMany.php
| | |____MorphOne.php
| | |____MorphOneOrMany.php
| | |____MorphPivot.php
| | |____MorphTo.php
| | |____MorphToMany.php
| | |____Pivot.php
| | |____Relation.php
| |____Scope.php
| |____SoftDeletes.php
| |____SoftDeletingScope.php
|____Events
| |____ConnectionEvent.php
| |____QueryExecuted.php
| |____StatementPrepared.php
| |____TransactionBeginning.php
| |____TransactionCommitted.php
| |____TransactionRolledBack.php
|____Grammar.php
|____Migrations
| |____DatabaseMigrationRepository.php
| |____Migration.php
| |____MigrationCreator.php
| |____MigrationRepositoryInterface.php
| |____Migrator.php
| |____stubs
| | |____blank.stub
| | |____create.stub
| | |____update.stub
|____MigrationServiceProvider.php
|____MySqlConnection.php
|____PostgresConnection.php
|____Query
| |____Builder.php
| |____Expression.php
| |____Grammars
| | |____Grammar.php
| | |____MySqlGrammar.php
| | |____PostgresGrammar.php
| | |____SQLiteGrammar.php
| | |____SqlServerGrammar.php
| |____JoinClause.php
| |____JsonExpression.php
| |____Processors
| | |____MySqlProcessor.php
| | |____PostgresProcessor.php
| | |____Processor.php
| | |____SQLiteProcessor.php
| | |____SqlServerProcessor.php
|____QueryException.php
|____README.md
|____Schema
| |____Blueprint.php
| |____Builder.php
| |____Grammars
| | |____ChangeColumn.php
| | |____Grammar.php
| | |____MySqlGrammar.php
| | |____PostgresGrammar.php
| | |____RenameColumn.php
| | |____SQLiteGrammar.php
| | |____SqlServerGrammar.php
| |____MySqlBuilder.php
| |____PostgresBuilder.php
| |____SQLiteBuilder.php
| |____SqlServerBuilder.php
|____Seeder.php
参考

Eloquent: 修改器 https://laravel-china.org/docs/laravel/5.7/eloquent-mutators/2297

__get()使用说明 http://php.net/manual/zh/language.oop5.overloading.php#object.get

未完待续

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29898.html

相关文章

  • 十五个常用的 Laravel 集合(Collection)

    摘要:将返回通过回调真值测试的第一个项的键。方法将集合分割为多个给定大小的较小集合。它可用于在任何位置的调试和查找集合内的内容。方法用于遍历整个集合。这对集合同样有效。它将导致由第二个参数的值作为键的集合。它接受回调并传递并将集合传递给它。 showImg(https://segmentfault.com/img/remote/1460000018924972); 文章转自:https://...

    alphahans 评论0 收藏0
  • laravel 数据迁移与 Eloquent ORM

    摘要:同时使用数据迁移管理数据库,可以与团队进行共享以及编辑。实际项目根据需求进行记录,以及选择存储方式。使用命令可以很方便的创建模型以及数据迁移。,参数在创建模型的同时也创建了数据迁移文件。参考资料数据库操作迁移快速入门。 导语 数据库可以说是后端开发最常用,也是最重要的部分。laravel 提供了很实用的 Eloquent ORM 模型类,简单、直观的与数据库进行交互。同时使用数据迁移管...

    tulayang 评论0 收藏0
  • 分享 10 个你可能不知道的 Laravel Eloquent 小技巧

    摘要:是一个功能丰富的框架。但是,你无法从官方文档中找到所有可用的功能。例数据库又插入一条为的数据。也很乐意听到你对此的看法和想法。你可以在上找到我。 showImg(https://segmentfault.com/img/remote/1460000017973901?w=800&h=511); Laravel 是一个功能丰富的框架。但是,你无法从官方文档中找到所有可用的功能。以下是一些...

    Simon_Zhou 评论0 收藏0
  • Eloquent中封装URL属性来统一管理视图中的路由

    摘要:在中封装属性来统一管理视图中的路由在应用程序中拥有数十个甚至数百个视图并不罕见。很快就会对视图中使用的路由管理失控。可以想象如果你在视图中做了多少这样的事情。这样做的好处在于,你修改视图中的任何路由,只需编辑两个文件,而不是数百个。 在Eloquent中封装URL属性来统一管理视图中的路由 在 Laravel 应用程序中拥有数十个甚至数百个视图并不罕见。很快就会对视图中使用的路由管理失...

    Kerr1Gan 评论0 收藏0
  • laravel5.5手写教程4Eloquent ORM分页及软删除

    摘要:从而达到了软删除。不过,你可以通过在查询中调用方法来强制查询已被软删除的模型方法也可以被用在关联查询只取出软删除数据会只取出软删除数据恢复被软删除的模型有时候你可能希望取消删除一个已被软删除的模型。 Laravel 有三宝,路由、容器和 Eloquent ORM,Eloquent ORM。我个人一直比较推荐于在实际操作中学习,之前简单了解了路由和Eloquent ORM的基本用法,今天...

    mindwind 评论0 收藏0

发表评论

0条评论

james

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<