0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

いいねとリプライ

Posted at

コメント機能

ルーティング

routes/web.php
// ログイン状態
Route::group(['middleware' => 'auth'], function() {
 // 省略
 // コメント関連
 Route::resource('comments', 'CommentsController', ['only' => ['store']]);
});

Model

app/Comment.php
 public function commentStore(Int $user_id, Array $data)
 {
 $this->user_id = $user_id;
 $this->tweet_id = $data['tweet_id'];
 $this->text = $data['text'];
 $this->save();
 return;
 }

Controller

php artisan make:controller CommentsController --resource
app/Http/Controllers/CommentsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Comment;
class CommentsController extends Controller
{
 public function store(Request $request, Comment $comment)
 {
 $user = auth()->user();
 $data = $request->all();
 $validator = Validator::make($data, [
 'tweet_id' =>['required', 'integer'],
 'text' => ['required', 'string', 'max:140']
 ]);
 $validator->validate();
 $comment->commentStore($user->id, $data);
 return back();
 }
}

いいね機能

いいね機能では一人につき1回。
まだいいねを付けていない投稿に対してはいいねを保存。
逆に既にいいねを付けているツイートに対して再度いいねを押すと削除するという仕様。

ルーティング

routes/web.php
// ログイン状態
Route::group(['middleware' => 'auth'], function() {
 // 省略
 // いいね関連
 Route::resource('favorites', 'FavoritesController', ['only' => ['store', 'destroy']]);
});

Model

app/Favorite.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Favorite extends Model
{
 public $timestamps = false;
 // いいねしているかどうかの判定処理
 public function isFavorite(Int $user_id, Int $tweet_id)
 {
 return (boolean) $this->where('user_id', $user_id)->where('tweet_id', $tweet_id)->first();
 }
 public function storeFavorite(Int $user_id, Int $tweet_id)
 {
 $this->user_id = $user_id;
 $this->tweet_id = $tweet_id;
 $this->save();
 return;
 }
 public function destroyFavorite(Int $favorite_id)
 {
 return $this->where('id', $favorite_id)->delete();
 }
}

いいねを押した際にツイートに対して既にいいね済みであればfalse、逆に存在しなければtrue

Controller

php artisan make:controller FavoritesController --resource
app/Http/Controllers/FavoritesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Favorite;
class FavoritesController extends Controller
{
 public function store(Request $request, Favorite $favorite)
 {
 $user = auth()->user();
 $tweet_id = $request->tweet_id;
 $is_favorite = $favorite->isFavorite($user->id, $tweet_id);
 if(!$is_favorite) {
 $favorite->storeFavorite($user->id, $tweet_id);
 return back();
 }
 return back();
 }
 public function destroy(Favorite $favorite)
 {
 $user_id = $favorite->user_id;
 $tweet_id = $favorite->tweet_id;
 $favorite_id = $favorite->id;
 $is_favorite = $favorite->isFavorite($user_id, $tweet_id);
 if($is_favorite) {
 $favorite->destroyFavorite($favorite_id);
 return back();
 }
 return back();
 }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?