Procházet zdrojové kódy

Add moderation status to comments

axkuhta před 1 rokem
rodič
revize
bf7167a784

+ 3 - 1
app/Console/Commands/LoadSampleData.php

@@ -3,6 +3,7 @@
 namespace App\Console\Commands;
 
 use Illuminate\Console\Command;
+use App\Models\Comment;
 use App\Models\Article;
 use App\Models\Author;
 use App\Models\Book;
@@ -39,7 +40,8 @@ class LoadSampleData extends Command
         $article->comments()->create([
 			"name" => "somebody",
 			"email" => "a@mail.ru",
-			"content" => "Ну и дичь"
+			"content" => "Ну и дичь",
+			"status" => Comment::STATUS_PUBLISHED
         ]);
 
 		$article = new Article;

+ 1 - 1
app/Http/Controllers/ArticleController.php

@@ -31,7 +31,7 @@ class ArticleController extends Controller
 	function view(Article $article) {
 		return view("article", [
 			"article" => $article->load([
-				"comments" => function($query) { $query->recent(); }
+				"comments" => function($query) { $query->published()->recent(); }
 			])
 		]);
 	}

+ 10 - 1
app/Models/Comment.php

@@ -9,13 +9,22 @@ class Comment extends Model
 {
 	use SoftDeletes;
 
+	const STATUS_PENDING	= 0;
+	const STATUS_PUBLISHED	= 1;
+	const STATUS_REJECTED	= 2;
+
 	// Наличие fillable требуется только при использовании mass assignment, т.е. создание записи из ассоциативного массива
 	protected $fillable = [
 		"name",
 		"email",
-		"content"
+		"content",
+		"status"
 	];
 
+	function scopePublished($query) {
+		$query->where("status", static::STATUS_PUBLISHED);
+	}
+
 	function scopeRecent($query) {
 		$query->orderBy("created_at", "desc");
 	}

+ 2 - 1
database/migrations/2023_11_28_191724_create_comments_table.php

@@ -14,11 +14,12 @@ return new class extends Migration
         Schema::create('comments', function (Blueprint $table) {
             $table->id();
             $table->timestamps();
+            $table->softDeletes();
             $table->string("name");
             $table->string("email");
             $table->text("content");
+            $table->tinyInteger("status")->unsigned()->default(0);
             $table->morphs("commentable");
-            $table->softDeletes();
         });
     }