user_id = Auth::user()->id; } $entity->account_id = Auth::user()->account_id; } elseif ($account_id || $user_id) { if ($user_id && !$ignore_user_id) { $entity->user_id = $user_id; } $entity->account_id = $account_id; } else { App::abort(500); } return $entity; } /** * Validate the model instance. * * @param $data * * @return bool */ public function validate($data) { $v = Validator::make($data, $this->rules, $this->messages); if ($v->fails()) { $this->errors = $v->messages(); return false; } // validation pass return true; } /** * Gets the validation error messages. * * @param bool $returnArray * * @return mixed */ public function errors($returnArray = true) { return $returnArray ? $this->errors->toArray() : $this->errors; } /** * Get a formatted date. * * @param $field * @param string $format * * @return bool|null|string */ public function getFormattedDate($field, $format = 'd-m-Y H:i') { return $this->$field === null ? null : date($format, strtotime($this->$field)); } /** * Ensures each query looks for account_id * * @param $query * @param bool $accountId * @param Request $request * @return mixed */ public function scopeScope($query, $accountId = false) { /* * GOD MODE - DON'T UNCOMMENT! * returning $query before adding the account_id condition will let you * browse all events etc. in the system. * //return $query; */ if (!$accountId) { $accountId = Auth::user()->account_id; } $table = $this->getTable(); $query->where(function ($query) use ($accountId, $table) { $query->whereRaw(\DB::raw('(' . $table . '.account_id = ' . $accountId . ')')); }); return $query; } }