forked from summerblue/larabbs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
22dc097
commit 90e5cc7
Showing
17 changed files
with
410 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
database/migrations/2019_09_20_093630_create_replies_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.