这是我的 User.php 模型
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstname','lastname', 'email','image', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
这是输出
Array
(
[0] => Array
(
[id] => 1
[firstname] => admin
[lastname] => Gomez
[email] => [email protected]
[image] => admin.png
[created_at] => 2019-11-10 04:30:01
[updated_at] => 2019-11-10 14:03:38
)
)
我只想要使用 eloquent Model 的 getter setter 方法获取 url 的完整路径
像这样
Array
(
[0] => Array
(
[id] => 1
[firstname] => admin
[lastname] => Gomez
[email] => [email protected]
[image] => public/images/admin.png
[created_at] => 2019-11-10 04:30:01
[updated_at] => 2019-11-10 14:03:38
)
)
我还想添加另一个字段,如全名(如 [全名] => admin gomez)
我也想要这个
Array
(
[0] => Array
(
[id] => 1
[firstname] => admin
[lastname] => Gomez
[email] => [email protected]
[image] => admin.png
[created_at] => 2019-11-10 04:30:01
[updated_at] => 2019-11-10 14:03:38
[fullname] => admin gomez
)
)
谢谢
请在您的模型中定义这两种方法:
protected $append = ['full_name'];
/**
* Get the user's full name.
*
* @return string
*/
public function getFullNameAttribute()
{
return "{$this->first_name} {$this->last_name}";
}
/**
* Get full image path
*
* @return string
*/
public function getImageAttribute()
{
if ($this->image) {
return public_path($this->image);
}
return '';
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句