添加话题

This commit is contained in:
fthvgb1 2018-01-01 12:00:45 +08:00
parent f38cd8a3c5
commit 3c525feee1
19 changed files with 541 additions and 1 deletions

View File

@ -0,0 +1,58 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\TopicRequest;
use App\Models\Topic;
class TopicsController extends Controller
{
public function __construct()
{
$this->middleware('auth', ['except' => ['index', 'show']]);
}
public function index()
{
$topics = Topic::paginate();
return view('topics.index', compact('topics'));
}
public function show(Topic $topic)
{
return view('topics.show', compact('topic'));
}
public function create(Topic $topic)
{
return view('topics.create_and_edit', compact('topic'));
}
public function store(TopicRequest $request)
{
$topic = Topic::create($request->all());
return redirect()->route('topics.show', $topic->id)->with('message', 'Created successfully.');
}
public function edit(Topic $topic)
{
$this->authorize('update', $topic);
return view('topics.create_and_edit', compact('topic'));
}
public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());
return redirect()->route('topics.show', $topic->id)->with('message', 'Updated successfully.');
}
public function destroy(Topic $topic)
{
$this->authorize('destroy', $topic);
$topic->delete();
return redirect()->route('topics.index')->with('message', 'Deleted successfully.');
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class Request extends FormRequest
{
public function authorize()
{
// Using policy for Authorization
return true;
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace App\Http\Requests;
class TopicRequest 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
];
}
}

19
app/Models/Model.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model as EloquentModel;
class Model extends EloquentModel
{
public function scopeRecent($query)
{
return $query->orderBy('id', 'desc');
}
public function scopeOrdered($query)
{
return $query->orderBy('order', 'desc');
}
}

8
app/Models/Topic.php Normal file
View File

@ -0,0 +1,8 @@
<?php
namespace App\Models;
class Topic extends Model
{
protected $fillable = ['"title', 'body', 'user_id', 'category_id', 'reply_count', 'view_count', 'last_reply_user_id', 'order', 'excerpt', 'slug'];
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Observers;
use App\Models\Topic;
// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
class TopicObserver
{
public function creating(Topic $topic)
{
//
}
public function updating(Topic $topic)
{
//
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Observers;
use App\Models\User;
// creating, created, updating, updated, saving,
// saved, deleting, deleted, restoring, restored
class UserObserver
{
public function creating(User $user)
{
//
}
public function updating(User $user)
{
//
}
}

22
app/Policies/Policy.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
class Policy
{
use HandlesAuthorization;
public function __construct()
{
//
}
public function before($user, $ability)
{
// if ($user->isSuperAdmin()) {
// return true;
// }
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace App\Policies;
use App\Models\Topic;
use App\Models\User;
class TopicPolicy extends Policy
{
public function update(User $user, Topic $topic)
{
// return $topic->user_id == $user->id;
return true;
}
public function destroy(User $user, Topic $topic)
{
return true;
}
}

View File

@ -14,6 +14,7 @@ class AuthServiceProvider extends ServiceProvider
* @var array
*/
protected $policies = [
\App\Models\Topic::class => \App\Policies\TopicPolicy::class,
'App\Model' => 'App\Policies\ModelPolicy',
User::class => UserPolicy::class,
];

View File

@ -0,0 +1,9 @@
<?php
use Faker\Generator as Faker;
$factory->define(App\Models\Topic::class, function (Faker $faker) {
return [
// 'name' => $faker->name,
];
});

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateTopicsTable extends Migration
{
public function up()
{
Schema::create('topics', function (Blueprint $table) {
$table->increments('id');
$table->string('"title')->index();
$table->text('body');
$table->integer('user_id')->unsigned()->index();
$table->integer('category_id')->unsigned()->index();
$table->integer('reply_count')->unsigned()->default(0);
$table->integer('view_count')->unsigned()->default(0);
$table->integer('last_reply_user_id')->unsigned()->default(0);
$table->integer('order')->unsigned()->default(0);
$table->text('excerpt');
$table->string('slug')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::drop('topics');
}
}

View File

@ -12,5 +12,6 @@ class DatabaseSeeder extends Seeder
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(TopicsTableSeeder::class);
}
}

View File

@ -0,0 +1,20 @@
<?php
use App\Models\Topic;
use Illuminate\Database\Seeder;
class TopicsTableSeeder extends Seeder
{
public function run()
{
$topics = factory(Topic::class)->times(50)->make()->each(function ($topic, $index) {
if ($index == 0) {
// $topic->field = 'value';
}
});
Topic::insert($topics->toArray());
}
}

View File

@ -0,0 +1,10 @@
@if (count($errors) > 0)
<div class="alert alert-danger">
<p>There were some problems with your input.</p>
<ul>
@foreach ($errors->all() as $error)
<li><i class="glyphicon glyphicon-remove"></i> {{ $error }}</li>
@endforeach
</ul>
</div>
@endif

View File

@ -0,0 +1,98 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<h1>
<i class="glyphicon glyphicon-edit"></i> Topic /
@if($topic->id)
Edit #{{$topic->id}}
@else
Create
@endif
</h1>
</div>
@include('common.error')
<div class="panel-body">
@if($topic->id)
<form action="{{ route('topics.update', $topic->id) }}" method="POST" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT">
@else
<form action="{{ route('topics.store') }}" method="POST" accept-charset="UTF-8">
@endif
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="title-field">"title</label>
<input class="form-control" type="text" name="title" id=title-field"
value="{{ old('"title', $topic->title ) }}"/>
</div>
<div class="form-group">
<label for="body-field">Body</label>
<textarea name="body" id="body-field" class="form-control"
rows="3">{{ old('body', $topic->body ) }}</textarea>
</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', $topic->user_id ) }}"/>
</div>
<div class="form-group">
<label for="category_id-field">Category_id</label>
<input class="form-control" type="text" name="category_id"
id="category_id-field"
value="{{ old('category_id', $topic->category_id ) }}"/>
</div>
<div class="form-group">
<label for="reply_count-field">Reply_count</label>
<input class="form-control" type="text" name="reply_count"
id="reply_count-field"
value="{{ old('reply_count', $topic->reply_count ) }}"/>
</div>
<div class="form-group">
<label for="view_count-field">View_count</label>
<input class="form-control" type="text" name="view_count" id="view_count-field"
value="{{ old('view_count', $topic->view_count ) }}"/>
</div>
<div class="form-group">
<label for="last_reply_user_id-field">Last_reply_user_id</label>
<input class="form-control" type="text" name="last_reply_user_id"
id="last_reply_user_id-field"
value="{{ old('last_reply_user_id', $topic->last_reply_user_id ) }}"/>
</div>
<div class="form-group">
<label for="order-field">Order</label>
<input class="form-control" type="text" name="order" id="order-field"
value="{{ old('order', $topic->order ) }}"/>
</div>
<div class="form-group">
<label for="excerpt-field">Excerpt</label>
<textarea name="excerpt" id="excerpt-field" class="form-control"
rows="3">{{ old('excerpt', $topic->excerpt ) }}</textarea>
</div>
<div class="form-group">
<label for="slug-field">Slug</label>
<input class="form-control" type="text" name="slug" id="slug-field"
value="{{ old('slug', $topic->slug ) }}"/>
</div>
<div class="well well-sm">
<button type="submit" class="btn btn-primary">Save</button>
<a class="btn btn-link pull-right" href="{{ route('topics.index') }}"><i
class="glyphicon glyphicon-backward"></i> Back</a>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,83 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<h1>
<i class="glyphicon glyphicon-align-justify"></i> Topic
<a class="btn btn-success pull-right" href="{{ route('topics.create') }}"><i
class="glyphicon glyphicon-plus"></i> Create</a>
</h1>
</div>
<div class="panel-body">
@if($topics->count())
<table class="table table-condensed table-striped">
<thead>
<tr>
<th class="text-center">#</th>
<th>"title</th>
<th>Body</th>
<th>User_id</th>
<th>Category_id</th>
<th>Reply_count</th>
<th>View_count</th>
<th>Last_reply_user_id</th>
<th>Order</th>
<th>Excerpt</th>
<th>Slug</th>
<th class="text-right">OPTIONS</th>
</tr>
</thead>
<tbody>
@foreach($topics as $topic)
<tr>
<td class="text-center"><strong>{{$topic->id}}</strong></td>
<td>{{$topic->title}}</td>
<td>{{$topic->body}}</td>
<td>{{$topic->user_id}}</td>
<td>{{$topic->category_id}}</td>
<td>{{$topic->reply_count}}</td>
<td>{{$topic->view_count}}</td>
<td>{{$topic->last_reply_user_id}}</td>
<td>{{$topic->order}}</td>
<td>{{$topic->excerpt}}</td>
<td>{{$topic->slug}}</td>
<td class="text-right">
<a class="btn btn-xs btn-primary" href="{{ route('topics.show', $topic->id) }}">
<i class="glyphicon glyphicon-eye-open"></i>
</a>
<a class="btn btn-xs btn-warning" href="{{ route('topics.edit', $topic->id) }}">
<i class="glyphicon glyphicon-edit"></i>
</a>
<form action="{{ route('topics.destroy', $topic->id) }}" method="POST"
style="display: inline;"
onsubmit="return confirm('Delete? Are you sure?');">
{{csrf_field()}}
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-xs btn-danger"><i
class="glyphicon glyphicon-trash"></i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
{!! $topics->render() !!}
@else
<h3 class="text-center alert alert-info">Empty!</h3>
@endif
</div>
</div>
</div>
</div>
@endsection

View File

@ -0,0 +1,64 @@
@extends('layouts.app')
@section('content')
<div class="container">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<h1>Topic / Show #{{ $topic->id }}</h1>
</div>
<div class="panel-body">
<div class="well well-sm">
<div class="row">
<div class="col-md-6">
<a class="btn btn-link" href="{{ route('topics.index') }}"><i
class="glyphicon glyphicon-backward"></i> Back</a>
</div>
<div class="col-md-6">
<a class="btn btn-sm btn-warning pull-right"
href="{{ route('topics.edit', $topic->id) }}">
<i class="glyphicon glyphicon-edit"></i> Edit
</a>
</div>
</div>
</div>
<label>"title</label>
<p>
{{ $topic->title }}
</p> <label>Body</label>
<p>
{{ $topic->body }}
</p> <label>User_id</label>
<p>
{{ $topic->user_id }}
</p> <label>Category_id</label>
<p>
{{ $topic->category_id }}
</p> <label>Reply_count</label>
<p>
{{ $topic->reply_count }}
</p> <label>View_count</label>
<p>
{{ $topic->view_count }}
</p> <label>Last_reply_user_id</label>
<p>
{{ $topic->last_reply_user_id }}
</p> <label>Order</label>
<p>
{{ $topic->order }}
</p> <label>Excerpt</label>
<p>
{{ $topic->excerpt }}
</p> <label>Slug</label>
<p>
{{ $topic->slug }}
</p>
</div>
</div>
</div>
</div>
@endsection

View File

@ -32,3 +32,4 @@ Route::get('password/reset', 'Auth\ForgotPasswordController@showLinkRequestForm'
Route::post('password/email', 'Auth\ForgotPasswordController@sendResetLinkEmail')->name('password.email');
Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm')->name('password.reset');
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
Route::resource('topics', 'TopicsController', ['only' => ['index', 'show', 'create', 'store', 'update', 'edit', 'destroy']]);