public function actionLike() { if (!\Yii::$app->getRequest()->getIsPost()) { return 'try agaen'; } if (\Yii::$app->getRequest()->getHeaders()->get('x-like') !== 'True') { return 'try agaen'; } $pictureId = \Yii::$app->getRequest()->post('p'); $hash = \Yii::$app->getRequest()->post('h'); $picture = Picture::find()->where(['id' => $pictureId])->one(); /* @var Picture $picture */ if ($picture == null) { return 'try agaen'; } if ($picture->getLikeHash() !== $hash) { return 'try agaen'; } $like = Like::find()->where(['ip' => \Yii::$app->getRequest()->getUserIP(), 'pictureId' => $picture->getPrimaryKey()])->count(); if ($like > 0) { return 'pls stap'; } $like = new Like(); $like->ip = \Yii::$app->getRequest()->getUserIP(); $like->pictureId = $picture->getPrimaryKey(); $like->save(); $picture->likeCount += 1; $picture->save(); return '+' . $picture->likeCount; }
/** * Добавить лайк к сообщению * @return mixed */ public function actionNewLike() { $msg = Yii::$app->request->post(); $user_set_like = (int) Like::find()->where(['message_id' => $msg['message_id'], 'user_name' => $this->current_user])->count(); if ($user_set_like === 0) { $like = new Like(); $like->message_id = $msg['message_id']; $like->user_name = $msg['user_name']; $like->save(); return Like::find()->where(['message_id' => $msg['message_id']])->count(); } else { return false; } }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $like = Like::find($id); if ($like) { if ($like->delete()) { return response(['message' => 'successfull']); } else { return $this->errorDelete(); } } else { return $this->notFoundResponse(); } }
public function actionUnlike($id) { $apartment = $this->findModel($id); if ($apartment) { $like = Like::findOne(['apartment_id' => $apartment->id, 'author_id' => \Yii::$app->user->id]); if ($like) { $like->delete(); return 'ok'; } // $like->unlink('apartment', $apartment); } return 'error'; }
public function search($input) { $query = Like::query(); $columns = Schema::getColumnListing('likes'); $attributes = array(); foreach ($columns as $attribute) { if (isset($input[$attribute]) and !empty($input[$attribute])) { $query->where($attribute, $input[$attribute]); $attributes[$attribute] = $input[$attribute]; } else { $attributes[$attribute] = null; } } return [$query->get(), $attributes]; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Like::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $this->author_id = Yii::$app->user->isGuest ? 0 : \Yii::$app->user->id; $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } // grid filtering conditions $query->andFilterWhere(['like', 'id', $this->id])->andFilterWhere(['like', 'apartment_id', $this->apartment_id])->andFilterWhere(['like', 'url', $this->url])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'author_id', $this->author_id])->andFilterWhere(['like', 'updater_id', $this->updater_id])->andFilterWhere(['like', 'created_at', $this->created_at])->andFilterWhere(['like', 'updated_at', $this->updated_at]); return $dataProvider; }
public function like(Request $request, $id) { if (!$this->login_user) { $result = ['status' => 201, 'msg' => '请先登录']; } else { $query = Like::where('user_id', $this->login_user->id)->where('like_type', 'article')->where('like_id', $id); if ($query->first()) { $query->delete(); Article::find($id)->decrement('likes', 1); $result = ['status' => 200, 'type' => 'cancel', 'msg' => '已取消点赞']; } else { Like::create(['user_id' => $this->login_user->id, 'like_type' => 'article', 'like_id' => $id]); Article::find($id)->increment('likes', 1); $result = ['status' => 200, 'type' => 'success', 'msg' => '点赞成功']; } } return response()->json($result); }
/** * @return \yii\db\ActiveQuery */ public function getLikes() { return $this->hasMany(Like::className(), ['post_id' => 'post_id']); }
/** * @return \yii\db\ActiveQuery */ public function getLikes() { return $this->hasMany(Like::className(), ['message_id' => 'id']); }
/** * Finds the Like model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Like the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Like::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }