PHP code example of lookfeel / laravel-eloquent-append-automate

1. Go to this page and download the library: Download lookfeel/laravel-eloquent-append-automate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lookfeel / laravel-eloquent-append-automate example snippets


use Lookfeel\AppendAutomate\Database\Eloquent\Model;

class User extend Modal {
    protected $appends = [
        'first_letter',
        'firstname|lastname' => 'fullname',     // firstname 和 lastname 字段缺一不可,否则不返回 fullname
        'gender' => 'gender_text',  // gender 是一个 int 字段,0:女,1:男
        'status' => 'status_text', // status 是一个 int 字段,0:禁用,1:启用
        'gender_text' => 'access',
        'access' => 'access_text',
    ];
    public function getFirstLetterAttribute()
    {
        return substr($this->firstname, 0, 1);
    }
    public function getFullnameAttribute()
    {
        return $this->firstname . ' ' . $this->lastname;
    }
    public function getGenderTextAttribute()
    {
        return ['女', '男'][$this->gender];
    }
    public function getStatusTextAttribute()
    {
        return ['启用', '禁用'][$this->status];
    }
    public function getAccessAttribute()
    {
        return $this->gender_text === 'Female';
    }
    public function getAccessTextAttribute()
    {
        return $this->access ? 'can' : 'Can not';
    }
}

User::select(['id', 'firstname', 'gender'])->firstOrFail();

/***
{
    "id": 1,
    "first_letter": "T",
    "firstname": "Terran",
    "gender": 1,
    "gender_text": "男",
    "access": 1,
    "access_text": "can"
}
*/