這篇文章給大家介紹laravel 之 Eloquent 模型修改器和序列化,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

修改器
獲取
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
public function getFirstNameAttribute($value) {
return ucfirst($value);
}
}使用 Laravel 加密器 來加密一個被保存在數(shù)據(jù)庫中的值,當(dāng)你從 Eloquent 模型訪問該屬性時該值將被自動解密。
$user = App\User::find(1); $firstName = $user->first_name;
修改
public function setFirstNameAttribute ($value) {
$this->attributes['first_name'] = strtolower($value);
}$user = App\User::find(1); $user->first_name = 'Sally';
日期轉(zhuǎn)化器
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
}$user = App\User::find(1); $user->deleted_at = Carbon::now(); $user->save();
可在屬性上使用任何 Carbon 方法:
$user = App\User::find(1); echo $user->deleted_at->getTimestamp();
設(shè)置時間格式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model {
protected $dateFormat = 'U';
}屬性類型轉(zhuǎn)化
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $casts = [
'is_admin' => 'boolean',
];
}現(xiàn)在當(dāng)你訪問 is_admin 屬性時,它將會被轉(zhuǎn)換成布爾值,即便保存在數(shù)據(jù)庫里的值是一個整數(shù):
$user = App\User::find(1);
if ($user->is_admin) {
//
}支持的轉(zhuǎn)換的類型有:
integer
real
float
double
string
boolean
object
array
collection
date
datetime
timestamp
# protected $casts = [ # 'options' => 'array', # ]; $user = App\User::find(1); $options = $user->options; $options['key'] = 'value'; $user->options = $options; $user->save();
序列化模型或集合
序列化成數(shù)組
$user = App\User::with('roles')->first();
return $user->toArray();序列化成 JSON
$user = App\User::find(1); return $user->toJson(); // 或者 return (string) $user; // 自動調(diào)用 toJson // 或者 return App\User::all();
隱藏來自 json 的屬性
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $hidden = ['password'];
}<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $visible = ['first_name', 'last_name'];
}臨時隱藏
return $user->makeVisible('attribute')->toArray();
return $user->makeHidden('attribute')->toArray();添加參數(shù)到 json 中
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
protected $appends = ['is_admin'];
}# 在 appends 數(shù)組中的屬性也遵循模型中 visible 和 hidden 設(shè)置
public function getIsAdminAttribute() {
return $this->attributes['is_admin'] == 'yes';
}關(guān)于laravel 之 Eloquent 模型修改器和序列化就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享題目:laravel之Eloquent模型修改器和序列化-創(chuàng)新互聯(lián)
分享網(wǎng)址:http://chinadenli.net/article44/hpihe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、微信小程序、網(wǎng)頁設(shè)計公司、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容