1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- namespace App\Models;
- use Encore\Admin\Auth\Database\Administrator;
- use Encore\Admin\Facades\Admin;
- use Exception;
- use Illuminate\Database\Eloquent\Model;
- class Activity extends Model
- {
- public static $mode = 'api';
- //
- public static function boot ()
- {
- parent::boot();
- if(self::$mode == 'admin'){
- static::creating(function ($model){
- $model->creator = Admin::user()->id;
- });
- }
- if(self::$mode == 'admin'){
- static::updating(function ($model){
- $model->updator = Admin::user()->id;
- });
- }
- }
- public function setExtAttribute($ext)
- {
- if (is_array($ext)) {
- $this->attributes['ext'] = json_encode($ext);
- }
- }
- public function getExtAttribute($ext)
- {
- return json_decode($ext, true);
- }
- /**
- * 创建活动
- *
- *
- *
- */
- public static function store($data, $id = 0)
- {
- // var_dump($data['ext']);exit;
- self::$mode = 'api';
- if($id) {
- $model = self::find($id);
- if(!$model) {
- throw new Exception('该信息不存在');
- }
- }else {
- $model = new self();
- }
- $model->organiz_type = $data['organiz_type'];
- $model->activity_range_id = $data['activity_range_id'];
- $model->organiz_name = $data['organiz_name'];
- $model->title = $data['title'];
- $model->member_num = $data['member_num'];
- $model->content = $data['content'];
- $model->active_time = $data['activity_time']??'';
- $model->belong = $data['belong'];
- $model->member = $data['member'];
- $model->ext = $data['ext'];
- $res = $model->save();
- if($res) {
- return ["id" => $model->id];
- } else {
- return [];
- }
- }
- public function creater()
- {
- return $this->belongsTo(Administrator::class, 'creator');
- }
- public function updater()
- {
- return $this->belongsTo(Administrator::class, 'updator');
- }
- public function range()
- {
- return $this->belongsTo(ActivityRange::class, 'activity_range_id');
- }
- public function workstation()
- {
- return $this->belongsTo(ActivityWorkStation::class, 'belong','work_station_id');
- }
- }
|