亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

Eloquent: 修改器

james / 3045人閱讀

摘要:好了,廢話不多說(shuō),今天來(lái)說(shuō)一說(shuō)修改器。前兩天看一同事好像對(duì)這個(gè)修改器了解不多,所以今天就拿它作為入口,扒一扒其實(shí)現(xiàn)源代碼。前端就可以直接拿到結(jié)果同樣的,還有方法來(lái)定義一個(gè)修改器。

感覺(jué)好長(zhǎng)時(shí)間沒(méi)寫東西了,一方面主要是自己的角色發(fā)生了變化,每天要面對(duì)各種各樣的事情和突發(fā)事件,不能再有一個(gè)完整的長(zhǎng)時(shí)間讓自己靜下來(lái)寫代碼,或者寫文章。

另一方面現(xiàn)在公司技術(shù)棧不再停留在只有 Laravel + VUE 了,我們還有小程序、APP 等開發(fā),所以我關(guān)注的東西也就多了。

接下來(lái)我還是會(huì)繼續(xù)持續(xù)「高產(chǎn)」,把寫技術(shù)文章當(dāng)作一個(gè)習(xí)慣,堅(jiān)持下去。

好了,廢話不多說(shuō),今天來(lái)說(shuō)一說(shuō)「Eloquent: 修改器」。

一直想好好研究下 Eloquent。但苦于 Eloquent 有太多可研究的,無(wú)法找到一個(gè)切入點(diǎn)。前兩天看一同事好像對(duì)這個(gè)「Eloquent: 修改器」了解不多,所以今天就拿它作為入口,扒一扒其實(shí)現(xiàn)源代碼。

首先還是拿一個(gè) Demo 為例:

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

這個(gè)代碼比較簡(jiǎn)單,就是通過(guò)已有屬性 birthday,計(jì)算 Baby 幾歲了,得到 age 屬性。

前端就可以直接拿到結(jié)果:

return $baby->age;

同樣的,還有 setXxxAttribute 方法來(lái)定義一個(gè)修改器。

源代碼

讀代碼還是從使用入手,如上通過(guò) $baby->age 調(diào)用 age 屬性,這個(gè)屬性沒(méi)在類中定義,所以只能通過(guò) PHP 的魔術(shù)方法 __get() 調(diào)用了。

我們看看 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);
    }

    ...
}

重點(diǎn)自然就在第二個(gè) if 上,主要判斷 attributes 數(shù)組中是否包含該屬性,如果沒(méi)有,則會(huì)執(zhí)行函數(shù) $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");
}

這就對(duì)上了我們的 Demo 中自定義的函數(shù) getAgeAttribute(),也就返回 true 了。

接下來(lái)就是執(zhí)行函數(shù) $this->getAttributeValue($key),進(jìn)而執(zhí)行函數(shù):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 也是很簡(jiǎn)單的。

總結(jié)

好長(zhǎng)時(shí)間沒(méi)寫東西了,先從最簡(jiǎn)單的入手,練練手。解析 Eloquent 需要費(fèi)很多腦細(xì)胞,接下來(lái)的一段時(shí)間我會(huì)圍繞著這個(gè)主題好好研究下去,盡可能的全部解讀一遍::

.
|____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()使用說(shuō)明 http://php.net/manual/zh/language.oop5.overloading.php#object.get

未完待續(xù)

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/29898.html

相關(guān)文章

  • 十五個(gè)常用的 Laravel 集合(Collection)

    摘要:將返回通過(guò)回調(diào)真值測(cè)試的第一個(gè)項(xiàng)的鍵。方法將集合分割為多個(gè)給定大小的較小集合。它可用于在任何位置的調(diào)試和查找集合內(nèi)的內(nèi)容。方法用于遍歷整個(gè)集合。這對(duì)集合同樣有效。它將導(dǎo)致由第二個(gè)參數(shù)的值作為鍵的集合。它接受回調(diào)并傳遞并將集合傳遞給它。 showImg(https://segmentfault.com/img/remote/1460000018924972); 文章轉(zhuǎn)自:https://...

    alphahans 評(píng)論0 收藏0
  • laravel 數(shù)據(jù)遷移與 Eloquent ORM

    摘要:同時(shí)使用數(shù)據(jù)遷移管理數(shù)據(jù)庫(kù),可以與團(tuán)隊(duì)進(jìn)行共享以及編輯。實(shí)際項(xiàng)目根據(jù)需求進(jìn)行記錄,以及選擇存儲(chǔ)方式。使用命令可以很方便的創(chuàng)建模型以及數(shù)據(jù)遷移。,參數(shù)在創(chuàng)建模型的同時(shí)也創(chuàng)建了數(shù)據(jù)遷移文件。參考資料數(shù)據(jù)庫(kù)操作遷移快速入門。 導(dǎo)語(yǔ) 數(shù)據(jù)庫(kù)可以說(shuō)是后端開發(fā)最常用,也是最重要的部分。laravel 提供了很實(shí)用的 Eloquent ORM 模型類,簡(jiǎn)單、直觀的與數(shù)據(jù)庫(kù)進(jìn)行交互。同時(shí)使用數(shù)據(jù)遷移管...

    tulayang 評(píng)論0 收藏0
  • 分享 10 個(gè)你可能不知道的 Laravel Eloquent 小技巧

    摘要:是一個(gè)功能豐富的框架。但是,你無(wú)法從官方文檔中找到所有可用的功能。例數(shù)據(jù)庫(kù)又插入一條為的數(shù)據(jù)。也很樂(lè)意聽到你對(duì)此的看法和想法。你可以在上找到我。 showImg(https://segmentfault.com/img/remote/1460000017973901?w=800&h=511); Laravel 是一個(gè)功能豐富的框架。但是,你無(wú)法從官方文檔中找到所有可用的功能。以下是一些...

    Simon_Zhou 評(píng)論0 收藏0
  • Eloquent中封裝URL屬性來(lái)統(tǒng)一管理視圖中的路由

    摘要:在中封裝屬性來(lái)統(tǒng)一管理視圖中的路由在應(yīng)用程序中擁有數(shù)十個(gè)甚至數(shù)百個(gè)視圖并不罕見。很快就會(huì)對(duì)視圖中使用的路由管理失控??梢韵胂笕绻阍谝晥D中做了多少這樣的事情。這樣做的好處在于,你修改視圖中的任何路由,只需編輯兩個(gè)文件,而不是數(shù)百個(gè)。 在Eloquent中封裝URL屬性來(lái)統(tǒng)一管理視圖中的路由 在 Laravel 應(yīng)用程序中擁有數(shù)十個(gè)甚至數(shù)百個(gè)視圖并不罕見。很快就會(huì)對(duì)視圖中使用的路由管理失...

    Kerr1Gan 評(píng)論0 收藏0
  • laravel5.5手寫教程4Eloquent ORM分頁(yè)及軟刪除

    摘要:從而達(dá)到了軟刪除。不過(guò),你可以通過(guò)在查詢中調(diào)用方法來(lái)強(qiáng)制查詢已被軟刪除的模型方法也可以被用在關(guān)聯(lián)查詢只取出軟刪除數(shù)據(jù)會(huì)只取出軟刪除數(shù)據(jù)恢復(fù)被軟刪除的模型有時(shí)候你可能希望取消刪除一個(gè)已被軟刪除的模型。 Laravel 有三寶,路由、容器和 Eloquent ORM,Eloquent ORM。我個(gè)人一直比較推薦于在實(shí)際操作中學(xué)習(xí),之前簡(jiǎn)單了解了路由和Eloquent ORM的基本用法,今天...

    mindwind 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<