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
3535254
commit 3eaa3ee
Showing
20 changed files
with
511 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\Topic; | ||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\TopicRequest; | ||
|
||
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.'); | ||
} | ||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class Request extends FormRequest | ||
{ | ||
public function authorize() | ||
{ | ||
// Using policy for Authorization | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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 | ||
]; | ||
} | ||
} |
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,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'); | ||
} | ||
|
||
} |
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,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']; | ||
} |
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\Topic; | ||
|
||
// creating, created, updating, updated, saving, | ||
// saved, deleting, deleted, restoring, restored | ||
|
||
class TopicObserver | ||
{ | ||
public function creating(Topic $topic) | ||
{ | ||
// | ||
} | ||
|
||
public function updating(Topic $topic) | ||
{ | ||
// | ||
} | ||
} |
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\User; | ||
|
||
// creating, created, updating, updated, saving, | ||
// saved, deleting, deleted, restoring, restored | ||
|
||
class UserObserver | ||
{ | ||
public function creating(User $user) | ||
{ | ||
// | ||
} | ||
|
||
public function updating(User $user) | ||
{ | ||
// | ||
} | ||
} |
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,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; | ||
// } | ||
} | ||
} |
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\Topic; | ||
|
||
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; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
use Faker\Generator as Faker; | ||
|
||
$factory->define(App\Models\Topic::class, function (Faker $faker) { | ||
return [ | ||
// 'name' => $faker->name, | ||
]; | ||
}); |
30 changes: 30 additions & 0 deletions
30
database/migrations/2019_09_20_065528_create_topics_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,30 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Database\Migrations\Migration; | ||
|
||
class CreateTopicsTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('topics', function(Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('title')->index(); | ||
$table->text('body'); | ||
$table->bigInteger('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')->nullable(); | ||
$table->string('slug')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::drop('topics'); | ||
} | ||
} |
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,20 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Seeder; | ||
use App\Models\Topic; | ||
|
||
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()); | ||
} | ||
|
||
} | ||
|
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,10 @@ | ||
@if (count($errors) > 0) | ||
<div class="alert alert-danger"> | ||
<div class="mt-2"><b>有错误发生:</b></div> | ||
<ul class="mt-2 mb-2"> | ||
@foreach ($errors->all() as $error) | ||
<li><i class="glyphicon glyphicon-remove"></i> {{ $error }}</li> | ||
@endforeach | ||
</ul> | ||
</div> | ||
@endif |
Oops, something went wrong.