php Laravel Spatie状态获取最新状态

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Laravel Spatie状态获取最新状态相关的知识,希望对你有一定的参考价值。

// if called like this, it will return latest status name

$latest_compliance_self_declaration_status = $latest_compliance_self_declaration->rcu_case->status;
<?php

namespace App;

use App\Traits\Filterable;
use App\Traits\ListRenderable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\ModelStatus\HasStatuses;

class RcuCase extends Model
{
    use HasStatuses;
    use SoftDeletes;

    protected $fillable = [
        'created_by',
    ];

    protected $appends = [
        'latest_status_name',
        'latest_status',
    ];
    
    // append latest status name to json
    // return : PICKED_UP
    
    public function getLatestStatusNameAttribute()
    {
        return $this->status;
    }
    
    // append latest status to json
    // this will result to something like this
    /*
    "latest_status": {
                "id": 16,
                "name": "PICKED_UP",
                "reason": null,
                "model_id": 5,
                "model_type": "App\\RcuCase",
                "causer_id": 3,
                "created_at": "2018-08-16 11:14:52",
                "updated_at": "2018-08-16 11:14:52",
                "deleted_at": null,
            },
    
    */

    public function getLatestStatusAttribute()
    {
        return $this->status();
    }
}

以上是关于php Laravel Spatie状态获取最新状态的主要内容,如果未能解决你的问题,请参考以下文章

php Laravel上传Spatie Media Libary

php Laravel Spatie获得特定角色的用户

php Laravel Spatie更新用户和同步用户角色

php 具有Spatie Peermission包的Laravel Nova用户模型

PHP Laravel - 如何将 Spatie Async 用于并发函数

php WIP:Spatie Laravel许可 - 延长给予和撤销使用给定防护名称的权限