Skip to content

Commit e62c500

Browse files
committed
Allowing prefix to tables
1 parent f0ae9aa commit e62c500

11 files changed

+156
-32
lines changed

config/blog.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
/*
2121
|--------------------------------------------------------------------------
22-
| Route Middleware
22+
| Route middleware
2323
|--------------------------------------------------------------------------
2424
|
2525
| These middleware will be attached to every route. You can add your own
@@ -46,4 +46,16 @@
4646
'allow_comments' => true,
4747
'allow_guest_comments' => true,
4848
],
49+
50+
/*
51+
|--------------------------------------------------------------------------
52+
| Blog table prefix
53+
|--------------------------------------------------------------------------
54+
|
55+
| This is the prefix of the tables that will be created by the migrations
56+
| and used by the models shipped with this package.
57+
|
58+
*/
59+
60+
'table_prefix' => 'blog',
4961
];

database/migrations/2019_12_28_101010_create_posts_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreatePostsTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('posts', function (Blueprint $table) {
16+
Schema::create(config('blog.table_prefix', 'blog').'_posts', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->integer('category_id')->nullable();
1919
$table->index('category_id');
@@ -47,6 +47,6 @@ public function up()
4747
*/
4848
public function down()
4949
{
50-
Schema::dropIfExists('posts');
50+
Schema::dropIfExists(config('blog.table_prefix', 'blog').'_posts');
5151
}
5252
}

database/migrations/2019_12_28_101011_create_categories_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateCategoriesTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('categories', function (Blueprint $table) {
16+
Schema::create(config('blog.table_prefix', 'blog').'_categories', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->string('name');
1919
$table->string('slug')->nullable();
@@ -30,6 +30,6 @@ public function up()
3030
*/
3131
public function down()
3232
{
33-
Schema::dropIfExists('categories');
33+
Schema::dropIfExists(config('blog.table_prefix', 'blog').'_categories');
3434
}
3535
}

database/migrations/2019_12_28_101012_create_comments_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class CreateCommentsTable extends Migration
1313
*/
1414
public function up()
1515
{
16-
Schema::create('comments', function (Blueprint $table) {
16+
Schema::create(config('blog.table_prefix', 'blog').'_comments', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->integer('user_id')->nullable();
1919
$table->index('user_id');
@@ -34,6 +34,6 @@ public function up()
3434
*/
3535
public function down()
3636
{
37-
Schema::dropIfExists('comments');
37+
Schema::dropIfExists(config('blog.table_prefix', 'blog').'_comments');
3838
}
3939
}

database/migrations/2019_12_28_101013_create_tags_table.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public function up()
2020
$table->timestamps();
2121
});
2222

23-
Schema::create('taggables', function (Blueprint $table) {
24-
$table->increments('id');
25-
$table->integer('tag_id');
26-
$table->integer('taggable_id');
27-
$table->string('taggable_type');
28-
$table->timestamps();
29-
});
23+
if ( ! Schema::hasTable('taggables')) {
24+
Schema::create('taggables', function (Blueprint $table) {
25+
$table->increments('id');
26+
$table->integer('tag_id');
27+
$table->integer('taggable_id');
28+
$table->string('taggable_type');
29+
$table->timestamps();
30+
});
31+
}
3032
}
3133

3234
/**

src/Category.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,21 @@
66

77
class Category extends Model
88
{
9-
protected $table = 'categories';
10-
119
protected $primaryKey = 'id';
1210

1311
public $guarded = ['id'];
1412

1513
public $timestamps = true;
1614

15+
public function __construct(array $attributes = [])
16+
{
17+
if ( ! isset($this->table)) {
18+
$this->setTable(config('blog.table_prefix', 'blog').'_categories');
19+
}
20+
21+
parent::__construct($attributes);
22+
}
23+
1724
public function posts()
1825
{
1926
return $this->hasMany(Post::class, 'category_id', 'id');

src/Comment.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class Comment extends Model
1010
{
1111
use IsAuthorable;
1212

13-
protected $table = 'comments';
14-
1513
protected $primaryKey = 'id';
1614

1715
public $guarded = ['id'];
@@ -22,6 +20,15 @@ class Comment extends Model
2220
'is_approved' => 'boolean',
2321
];
2422

23+
public function __construct(array $attributes = [])
24+
{
25+
if ( ! isset($this->table)) {
26+
$this->setTable(config('blog.table_prefix', 'blog').'_comments');
27+
}
28+
29+
parent::__construct($attributes);
30+
}
31+
2532
public function commentable()
2633
{
2734
return $this->morphTo();

src/Controllers/CategoryController.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,35 @@
44

55
use Chriscreates\Blog\Category;
66
use Chriscreates\Blog\Requests\ValidateCategoryRequest;
7-
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Http\Request;
88

99
class CategoryController extends Controller
1010
{
1111
/**
12-
* Instantiate a new controller instance.
12+
* Display a listing of the resource.
1313
*
14-
* @return void
14+
* @return \Illuminate\Http\Response
1515
*/
16-
public function __construct()
16+
public function index()
17+
{
18+
}
19+
20+
/**
21+
* Show the form for creating a new resource.
22+
*
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function create()
1726
{
18-
$this->middleware('auth');
1927
}
2028

2129
/**
2230
* Store a newly created resource in storage.
2331
*
2432
* @param \Chriscreates\Blog\Requests\ValidateCategoryRequest $request
25-
* @return \Illuminate\Http\JsonResponse
33+
* @return \Illuminate\Http\Response
2634
*/
27-
public function store(ValidateCategoryRequest $request) : JsonResponse
35+
public function store(ValidateCategoryRequest $request)
2836
{
2937
$category = Category::create($request->only([
3038
'name',
@@ -34,4 +42,45 @@ public function store(ValidateCategoryRequest $request) : JsonResponse
3442

3543
return response()->json($category);
3644
}
45+
46+
/**
47+
* Display the specified resource.
48+
*
49+
* @param \Chriscreates\Blog\Category $category
50+
* @return \Illuminate\Http\Response
51+
*/
52+
public function show(Category $category)
53+
{
54+
}
55+
56+
/**
57+
* Show the form for editing the specified resource.
58+
*
59+
* @param \Chriscreates\Blog\Category $category
60+
* @return \Illuminate\Http\Response
61+
*/
62+
public function edit(Category $category)
63+
{
64+
}
65+
66+
/**
67+
* Update the specified resource in storage.
68+
*
69+
* @param \Illuminate\Http\Request $request
70+
* @param \Chriscreates\Blog\Requests\ValidateCategoryRequest $request
71+
* @return \Illuminate\Http\Response
72+
*/
73+
public function update(ValidateCategoryRequest $request, Category $category)
74+
{
75+
}
76+
77+
/**
78+
* Remove the specified resource from storage.
79+
*
80+
* @param \Chriscreates\Blog\Category $category
81+
* @return \Illuminate\Http\Response
82+
*/
83+
public function destroy(Category $category)
84+
{
85+
}
3786
}

src/Controllers/PostController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function index()
3535
->orderBy('created_at', 'desc')
3636
->paginate(10);
3737

38-
return view('blog.admin.posts.posts', compact('posts'));
38+
return view('admin.posts.posts', compact('posts'));
3939
}
4040

4141
/**
@@ -47,7 +47,7 @@ public function create()
4747
{
4848
$post = new Post;
4949

50-
return view('blog.admin.posts.post', [
50+
return view('admin.posts.post', [
5151
'post' => $post,
5252
'categories' => Category::all(),
5353
'tags' => Tag::all(),
@@ -77,7 +77,6 @@ public function store(ValidatePostRequest $request) : JsonResponse
7777
}
7878

7979
/**
80-
* TODO
8180
* Display the specified resource.
8281
*
8382
* @param \Chriscreates\Blog\Post $post
@@ -108,7 +107,7 @@ public function edit(Post $post)
108107
{
109108
$post->load('category', 'tags');
110109

111-
return view('blog.admin.posts.post', [
110+
return view('admin.posts.post', [
112111
'post' => $post,
113112
'categories' => Category::all(),
114113
'tags' => Tag::all(),

src/Controllers/TagController.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Chriscreates\Blog\Requests\ValidateTagRequest;
66
use Chriscreates\Blog\Tag;
77
use Illuminate\Http\JsonResponse;
8+
use Illuminate\Http\Request;
89

910
class TagController extends Controller
1011
{
@@ -18,11 +19,20 @@ public function __construct()
1819
$this->middleware('auth');
1920
}
2021

22+
/**
23+
* Display a listing of the resource.
24+
*
25+
* @return \Illuminate\Http\Response
26+
*/
27+
public function index() : JsonResponse
28+
{
29+
}
30+
2131
/**
2232
* Store a newly created resource in storage.
2333
*
2434
* @param \Chriscreates\Blog\Requests\ValidateTagRequest $request
25-
* @return \Illuminate\Http\JsonResponse
35+
* @return \Illuminate\Http\Response
2636
*/
2737
public function store(ValidateTagRequest $request) : JsonResponse
2838
{
@@ -33,4 +43,35 @@ public function store(ValidateTagRequest $request) : JsonResponse
3343

3444
return response()->json($tag);
3545
}
46+
47+
/**
48+
* Show the form for editing the specified resource.
49+
*
50+
* @param \Chriscreates\Blog\Tag $tag
51+
* @return \Illuminate\Http\Response
52+
*/
53+
public function edit(Tag $tag) : JsonResponse
54+
{
55+
}
56+
57+
/**
58+
* Update the specified resource in storage.
59+
*
60+
* @param \Chriscreates\Blog\Requests\ValidateTagRequest $request
61+
* @param \Chriscreates\Blog\Tag $tag
62+
* @return \Illuminate\Http\Response
63+
*/
64+
public function update(ValidateTagRequest $request, Tag $tag) : JsonResponse
65+
{
66+
}
67+
68+
/**
69+
* Remove the specified resource from storage.
70+
*
71+
* @param \Chriscreates\Blog\Tag $tag
72+
* @return \Illuminate\Http\Response
73+
*/
74+
public function destroy(Tag $tag) : JsonResponse
75+
{
76+
}
3677
}

0 commit comments

Comments
 (0)