如何在 Laravel 中使用 Getter Setter 方法?以及如何使用 Eloquent 模型添加字段?

维卡斯·卡塔里亚

这是我的 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 Laravel Eloquent 模型中使用 where

如何在抽象类中使用getter和setter

如何在getter和setter中使用Enums?

如何在JavaScript中使用setter和getter,遇到错误

如何在 Ruby 中使用 getter 和 setter?

如何在 Kotlin 中使用 Getter 和 Setter?

如何在模型中添加新字段并在Eloquent中使用它

如何在Laravel Eloquent中使用join?

如何使用Bytebuddy拦截字段访问(不使用getter / setter)

如何在两个相关模型中使用 PHP Laravel Eloquent groupBy 和 sum 方法?

如何在python中使用getter/setter方法来操作列表?

在Java中使用setter时如何同步getter

如何在 Laravel 中使用 eloquent 使用包含在数组字段中的搜索值

如何在Laravel中将元字段添加到Eloquent模型对象的集合中

如何在不使用Setter的情况下使用Getter

如何在输入字段模型属性的角度进行getter和setter方法?

我如何使用PojoCodecProvider忽略getter / setter

角度2:如何使用getter和setter方法将Input绑定到模型属性?

在Svelte Custom方法中使用getter / setter

在AngularJS Factory中使用Getter / Setter方法

在 main 方法中使用 getter 和 setter

如何在Angular 2中使用getter和setter实现BehaviorSubject

如何在Android中使用setter和getter存储全局变量?

如何在 Laravel Eloquent 中使用 GroupBy 和 Where 子句?

如何在Laravel Eloquent Eager Load函数中使用参数?

如何在 Laravel 5.8 中使用 Eloquent 进行查询?

如何在 eloquent laravel 中使用别名编写 SQL 查询?

如何在 Laravel eloquent builder 中使用原始 sql

我如何在Eloquent Laravel 5.5中使用Transaction