public function created(WorkOrder $model)
 {
     // 当一个工单保存时,并不会在工单对象中保存具体的内容,所有的工单内容都是在 WorkOrderMessage 中保存
     // 因此首次创建时会同时创建一个消息(WorkOrderMessage)
     $workOrderMessage = new WorkOrderMessage();
     $workOrderMessage->content = $this->request->input('content');
     $workOrderMessage->workOrder()->associate($model);
     $workOrderMessage->save();
     $user = \Auth::user();
     $user->myWorkOrderMessages()->save($workOrderMessage);
     // 工单发布人默认关注当前工单
     $user->involvements(User::INVOLVED_WORK_ORDER)->attach($model->id);
     foreach ($model->group->participants as $participant) {
         if ($participant->id == \Auth::id()) {
             continue;
         }
         $url = \get_uri_path(route('host.work.work-order.show', $model->id));
         $message = new Message();
         $message->title = '一个新的工单 #' . $model->id . ' 发布了';
         $message->content = view('host.communication.message.template.new-work-order-dynamic', ['workOrder' => $model, 'url' => $url]);
         $message->sendTo($participant);
     }
     // 创建历史
     $history = new WorkOrderHistory();
     $history->event_id = WorkOrderHistory::EVENT_WORK_ORDER_CREATE;
     $history->workOrder()->associate($model);
     $history->user()->associate(\Auth::user());
     $history->save();
 }
 public function store(Request $request, $id)
 {
     $validator = \Validator::make($request->all(), ['content' => 'required']);
     if ($validator->fails()) {
         return redirect()->back()->withErrors($validator->errors());
     }
     return \DB::transaction(function () use($id, $request) {
         $workOrder = WorkOrder::findOrFail($id);
         $message = new WorkOrderMessage();
         $message->content = $request->input('content');
         $message->workOrder()->associate($workOrder);
         $message->publisher()->associate(\Auth::user());
         $message->save();
         if ($request->ajax()) {
             return response()->json(['body' => $message->id]);
         }
         return redirect()->back()->with('create-success', 'work-order-message');
     });
 }
 /**
  * Register any other events for your application.
  *
  * @param  \Illuminate\Contracts\Events\Dispatcher  $events
  * @return void
  */
 public function boot(DispatcherContract $events)
 {
     parent::boot($events);
     WorkOrder::observe(WorkOrderObserver::class);
     WorkOrderMessage::observe(WorkOrderMessageObserver::class);
 }