Skip to content

Commit

Permalink
回复数据
Browse files Browse the repository at this point in the history
  • Loading branch information
summerblue committed Sep 20, 2019
1 parent 22dc097 commit 90e5cc7
Show file tree
Hide file tree
Showing 17 changed files with 410 additions and 1 deletion.
60 changes: 60 additions & 0 deletions app/Http/Controllers/RepliesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Controllers;

use App\Models\Reply;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\ReplyRequest;

class RepliesController extends Controller
{
public function __construct()
{
$this->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.');
}
}
41 changes: 41 additions & 0 deletions app/Http/Requests/ReplyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Requests;

class ReplyRequest extends Request
{
public function rules()
{
switch($this->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
];
}
}
18 changes: 18 additions & 0 deletions app/Models/Reply.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Models;

class Reply extends Model
{
protected $fillable = ['content'];

public function topic()
{
return $this->belongsTo(Topic::class);
}

public function user()
{
return $this->belongsTo(User::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public function isAuthorOf($model)
{
return $this->id == $model->user_id;
}

public function replies()
{
return $this->hasMany(Reply::class);
}
}
21 changes: 21 additions & 0 deletions app/Observers/ReplyObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Observers;

use App\Models\Reply;

// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored

class ReplyObserver
{
public function creating(Reply $reply)
{
//
}

public function updating(Reply $reply)
{
//
}
}
20 changes: 20 additions & 0 deletions app/Policies/ReplyPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Policies;

use App\Models\User;
use App\Models\Reply;

class ReplyPolicy extends Policy
{
public function update(User $user, Reply $reply)
{
// return $reply->user_id == $user->id;
return true;
}

public function destroy(User $user, Reply $reply)
{
return true;
}
}
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down
1 change: 1 addition & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand Down
15 changes: 15 additions & 0 deletions database/factories/ReplyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Models\Reply::class, function (Faker $faker) {

// 随机取一个月以内的时间
$time = $faker->dateTimeThisMonth();

return [
'content' => $faker->sentence(),
'created_at' => $time,
'updated_at' => $time,
];
});
23 changes: 23 additions & 0 deletions database/migrations/2019_09_20_093630_create_replies_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRepliesTable extends Migration
{
public function up()
{
Schema::create('replies', function(Blueprint $table) {
$table->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');
}
}
3 changes: 2 additions & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
37 changes: 37 additions & 0 deletions database/seeds/RepliesTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Seeder;
use App\Models\Reply;
use App\Models\User;
use App\Models\Topic;

class RepliesTableSeeder extends Seeder
{
public function run()
{
// 所有用户 ID 数组,如:[1,2,3,4]
$user_ids = User::all()->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());
}
}
56 changes: 56 additions & 0 deletions resources/views/replies/create_and_edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
@extends('layouts.app')

@section('content')

<div class="container">
<div class="col-md-10 offset-md-1">
<div class="card ">

<div class="card-header">
<h1>
Reply /
@if($reply->id)
Edit #{{ $reply->id }}
@else
Create
@endif
</h1>
</div>

<div class="card-body">
@if($reply->id)
<form action="{{ route('replies.update', $reply->id) }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT">
@else
<form action="{{ route('replies.store') }}" method="POST" accept-charset="UTF-8">
@endif

@include('common.error')

<input type="hidden" name="_token" value="{{ csrf_token() }}">


<div class="form-group">
<label for="topic_id-field">Topic_id</label>
<input class="form-control" type="text" name="topic_id" id="topic_id-field" value="{{ old('topic_id', $reply->topic_id ) }}" />
</div>
<div class="form-group">
<label for="user_id-field">User_id</label>
<input class="form-control" type="text" name="user_id" id="user_id-field" value="{{ old('user_id', $reply->user_id ) }}" />
</div>
<div class="form-group">
<label for="content-field">Content</label>
<textarea name="content" id="content-field" class="form-control" rows="3">{{ old('content', $reply->content ) }}</textarea>
</div>

<div class="well well-sm">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-link float-xs-right" href="{{ route('replies.index') }}"> <- Back</a>
</div>
</form>
</div>
</div>
</div>
</div>

@endsection
Loading

0 comments on commit 90e5cc7

Please sign in to comment.