catchAdmin/extend/catcher/traits/db/ScopeTrait.php

38 lines
981 B
PHP
Raw Normal View History

2020-06-21 10:48:11 +08:00
<?php
2020-11-29 09:29:14 +08:00
declare(strict_types=1);
2020-06-21 10:48:11 +08:00
/**
2020-06-21 10:50:05 +08:00
* @filename ScopeTrait.php
2020-06-21 10:48:11 +08:00
* @createdAt 2020/6/21
* @project https://github.com/yanwenwu/catch-admin
* @document http://doc.catchadmin.com
* @author JaguarJack <njphper@gmail.com>
* @copyright By CatchAdmin
* @license https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt
2020-06-21 10:50:05 +08:00
*/
namespace catcher\traits\db;
use catchAdmin\permissions\model\Users;
trait ScopeTrait
{
/**
* 创建人
*
* @time 2020年06月17日
* @param $query
* @return mixed
*/
public function scopeCreator($query)
{
2020-06-22 15:19:50 +08:00
if (property_exists($this, 'field') && in_array('creator_id', $this->field)) {
2020-06-21 10:50:05 +08:00
return $query->addSelectSub(function () {
$user = app(Users::class);
return $user->whereColumn($this->getTable() . '.creator_id', $user->getTable() . '.id')
->field('username');
}, 'creator');
}
return $query;
}
}