diff --git a/app/Http/Controllers/RepliesController.php b/app/Http/Controllers/RepliesController.php new file mode 100644 index 000000000..b3164b504 --- /dev/null +++ b/app/Http/Controllers/RepliesController.php @@ -0,0 +1,60 @@ +middleware('auth', ['except' => ['index', 'show']]); + } + + public function index() + { + $replies = Reply::paginate(); + return view('replies.index', compact('replies')); + } + + public function show(Reply $reply) + { + return view('replies.show', compact('reply')); + } + + public function create(Reply $reply) + { + return view('replies.create_and_edit', compact('reply')); + } + + public function store(ReplyRequest $request) + { + $reply = Reply::create($request->all()); + return redirect()->route('replies.show', $reply->id)->with('message', 'Created successfully.'); + } + + public function edit(Reply $reply) + { + $this->authorize('update', $reply); + return view('replies.create_and_edit', compact('reply')); + } + + public function update(ReplyRequest $request, Reply $reply) + { + $this->authorize('update', $reply); + $reply->update($request->all()); + + return redirect()->route('replies.show', $reply->id)->with('message', 'Updated successfully.'); + } + + public function destroy(Reply $reply) + { + $this->authorize('destroy', $reply); + $reply->delete(); + + return redirect()->route('replies.index')->with('message', 'Deleted successfully.'); + } +} \ No newline at end of file diff --git a/app/Http/Requests/ReplyRequest.php b/app/Http/Requests/ReplyRequest.php new file mode 100644 index 000000000..b592c3e21 --- /dev/null +++ b/app/Http/Requests/ReplyRequest.php @@ -0,0 +1,41 @@ +method()) + { + // CREATE + case 'POST': + { + return [ + // CREATE ROLES + ]; + } + // UPDATE + case 'PUT': + case 'PATCH': + { + return [ + // UPDATE ROLES + ]; + } + case 'GET': + case 'DELETE': + default: + { + return []; + } + } + } + + public function messages() + { + return [ + // Validation messages + ]; + } +} diff --git a/app/Models/Reply.php b/app/Models/Reply.php new file mode 100644 index 000000000..107f7a6cb --- /dev/null +++ b/app/Models/Reply.php @@ -0,0 +1,18 @@ +belongsTo(Topic::class); + } + + public function user() + { + return $this->belongsTo(User::class); + } +} diff --git a/app/Models/Topic.php b/app/Models/Topic.php index d1e291974..6fc9a7f29 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -8,6 +8,11 @@ class Topic extends Model 'title', 'body', 'category_id', 'excerpt', 'slug' ]; + public function replies() + { + return $this->hasMany(Reply::class); + } + public function category() { return $this->belongsTo(Category::class); diff --git a/app/Models/User.php b/app/Models/User.php index bb0fbfb3e..fda6cecde 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -32,4 +32,9 @@ public function isAuthorOf($model) { return $this->id == $model->user_id; } + + public function replies() + { + return $this->hasMany(Reply::class); + } } diff --git a/app/Observers/ReplyObserver.php b/app/Observers/ReplyObserver.php new file mode 100644 index 000000000..c646a8e63 --- /dev/null +++ b/app/Observers/ReplyObserver.php @@ -0,0 +1,21 @@ +user_id == $user->id; + return true; + } + + public function destroy(User $user, Reply $reply) + { + return true; + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 330b6b265..a7e2da1c1 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -24,6 +24,7 @@ public function register() public function boot() { \App\Models\User::observe(\App\Observers\UserObserver::class); + \App\Models\Reply::observe(\App\Observers\ReplyObserver::class); \App\Models\Topic::observe(\App\Observers\TopicObserver::class); // diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 5a84ff44f..eed597b76 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,6 +13,7 @@ class AuthServiceProvider extends ServiceProvider * @var array */ protected $policies = [ + \App\Models\Reply::class => \App\Policies\ReplyPolicy::class, \App\Models\Topic::class => \App\Policies\TopicPolicy::class, // 'App\Model' => 'App\Policies\ModelPolicy', ]; diff --git a/database/factories/ReplyFactory.php b/database/factories/ReplyFactory.php new file mode 100644 index 000000000..7e3bf1386 --- /dev/null +++ b/database/factories/ReplyFactory.php @@ -0,0 +1,15 @@ +define(App\Models\Reply::class, function (Faker $faker) { + + // 随机取一个月以内的时间 + $time = $faker->dateTimeThisMonth(); + + return [ + 'content' => $faker->sentence(), + 'created_at' => $time, + 'updated_at' => $time, + ]; +}); diff --git a/database/migrations/2019_09_20_093630_create_replies_table.php b/database/migrations/2019_09_20_093630_create_replies_table.php new file mode 100644 index 000000000..f725581e8 --- /dev/null +++ b/database/migrations/2019_09_20_093630_create_replies_table.php @@ -0,0 +1,23 @@ +increments('id'); + $table->integer('topic_id')->unsigned()->default(0)->index(); + $table->bigInteger('user_id')->unsigned()->default(0)->index(); + $table->text('content'); + $table->timestamps(); + }); + } + + public function down() + { + Schema::drop('replies'); + } +} diff --git a/database/seeds/DatabaseSeeder.php b/database/seeds/DatabaseSeeder.php index 8224864e0..285d2de68 100644 --- a/database/seeds/DatabaseSeeder.php +++ b/database/seeds/DatabaseSeeder.php @@ -12,6 +12,7 @@ class DatabaseSeeder extends Seeder public function run() { $this->call(UsersTableSeeder::class); - $this->call(TopicsTableSeeder::class); + $this->call(TopicsTableSeeder::class); + $this->call(RepliesTableSeeder::class); } } diff --git a/database/seeds/RepliesTableSeeder.php b/database/seeds/RepliesTableSeeder.php new file mode 100644 index 000000000..5d894fca8 --- /dev/null +++ b/database/seeds/RepliesTableSeeder.php @@ -0,0 +1,37 @@ +pluck('id')->toArray(); + + // 所有话题 ID 数组,如:[1,2,3,4] + $topic_ids = Topic::all()->pluck('id')->toArray(); + + // 获取 Faker 实例 + $faker = app(Faker\Generator::class); + + $replys = factory(Reply::class) + ->times(1000) + ->make() + ->each(function ($reply, $index) + use ($user_ids, $topic_ids, $faker) + { + // 从用户 ID 数组中随机取出一个并赋值 + $reply->user_id = $faker->randomElement($user_ids); + + // 话题 ID,同上 + $reply->topic_id = $faker->randomElement($topic_ids); + }); + + // 将数据集合转换为数组,并插入到数据库中 + Reply::insert($replys->toArray()); + } +} diff --git a/resources/views/replies/create_and_edit.blade.php b/resources/views/replies/create_and_edit.blade.php new file mode 100644 index 000000000..894a35590 --- /dev/null +++ b/resources/views/replies/create_and_edit.blade.php @@ -0,0 +1,56 @@ +@extends('layouts.app') + +@section('content') + +