Skip to content

Commit cda572f

Browse files
author
shellus
committed
重构数据库增加tag
1 parent c7d18aa commit cda572f

12 files changed

+153
-101
lines changed

app/Category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected static function boot()
5050

5151
public function articles()
5252
{
53-
return $this->belongsToMany('App\Article')->withTimestamps();
53+
return $this->belongsToMany('App\Article', 'article_category', 'category_id', 'article_id')->withTimestamps();
5454
}
5555

5656
public function showUrl(){

app/Http/Controllers/ArticleController.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use App\Article;
66
use App\ArticleVote;
7+
use App\Category;
78
use App\SearchHistory;
9+
use App\Tag;
810
use Illuminate\Http\Request;
911

1012
use App\Http\Requests;
@@ -60,6 +62,27 @@ public function index()
6062
//
6163
}
6264

65+
public function tagIndex($id){
66+
$articles = Tag::findOrFail($id)->articles()->orderBy('articles.updated_at', 'DESC')->paginate(20);
67+
68+
$articles->load(['comments' => function ($query) {
69+
// $query->selectRaw('min(id) as id, article_id, count(*) as comments_count');
70+
// $query->groupBy('article_id');
71+
$query->orderBy('created_at','DESC');
72+
}]);
73+
return view('article.index', ['articles' => $articles]);
74+
}
75+
public function categoryIndex($id){
76+
$articles = Category::findOrFail($id)->articles()->orderBy('articles.updated_at', 'DESC')->paginate(20);
77+
78+
$articles->load(['comments' => function ($query) {
79+
// $query->selectRaw('min(id) as id, article_id, count(*) as comments_count');
80+
// $query->groupBy('article_id');
81+
$query->orderBy('created_at','DESC');
82+
}]);
83+
return view('article.index', ['articles' => $articles]);
84+
}
85+
6386
/**
6487
* Show the form for creating a new resource.
6588
*

app/Http/Controllers/CategoryController.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,6 @@ public function store(Request $request)
6969
*/
7070
public function show($id)
7171
{
72-
$articles = Category::findOrFail($id)->articles()->orderBy('articles.updated_at', 'DESC')->paginate(20);
73-
74-
$articles->load(['comments' => function ($query) {
75-
// $query->selectRaw('min(id) as id, article_id, count(*) as comments_count');
76-
// $query->groupBy('article_id');
77-
$query->orderBy('created_at','DESC');
78-
}]);
79-
return view('article.index', ['articles' => $articles]);
8072
}
8173

8274
/**
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class TagController extends Controller
8+
{
9+
10+
/**
11+
* Show the form for creating a new resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function create()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Store a newly created resource in storage.
22+
*
23+
* @param \Illuminate\Http\Request $request
24+
* @return \Illuminate\Http\Response
25+
*/
26+
public function store(Request $request)
27+
{
28+
//
29+
}
30+
31+
/**
32+
* Display the specified resource.
33+
*
34+
* @param int $id
35+
* @return \Illuminate\Http\Response
36+
*/
37+
public function show($id)
38+
{
39+
//
40+
}
41+
42+
/**
43+
* Show the form for editing the specified resource.
44+
*
45+
* @param int $id
46+
* @return \Illuminate\Http\Response
47+
*/
48+
public function edit($id)
49+
{
50+
//
51+
}
52+
53+
/**
54+
* Update the specified resource in storage.
55+
*
56+
* @param \Illuminate\Http\Request $request
57+
* @param int $id
58+
* @return \Illuminate\Http\Response
59+
*/
60+
public function update(Request $request, $id)
61+
{
62+
//
63+
}
64+
65+
/**
66+
* Remove the specified resource from storage.
67+
*
68+
* @param int $id
69+
* @return \Illuminate\Http\Response
70+
*/
71+
public function destroy($id)
72+
{
73+
//
74+
}
75+
}

app/Tag.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public function __construct(array $attributes = [])
2828
$attributes['type'] = 'tag';
2929
parent::__construct($attributes);
3030
}
31-
31+
public function showUrl(){
32+
return route('tag.show', [$this -> id]);
33+
}
3234
}

database/migrations/2016_09_24_114036_categories.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ public function up()
1717
$table->increments('id');
1818
$table->string('title');
1919
$table->string('slug')->default('');
20+
$table->string('type')->default('category'); // or 'tag'
2021
$table->string('description')->default(''); // 指导资源分类者的标签介绍
2122
$table->integer('parent_id')->default(0)->unsigned();
23+
$table->integer('logo_id')->nullable()->unsigned();
2224
$table->timestamps();
2325

26+
$table->foreign('logo_id')->references('id')->on('files')
27+
->onUpdate('cascade')->onDelete('cascade');
2428
});
2529
}
2630

database/migrations/2016_09_24_115211_article_category.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function up()
2424
$table->foreign('category_id')->references('id')->on('categories')
2525
->onUpdate('cascade')->onDelete('cascade');
2626

27-
$table->index(['article_id', 'category_id']);
27+
$table->unique(['article_id', 'category_id']);
2828
});
2929
}
3030

database/migrations/2017_01_04_173336_add_categorie_logo.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

database/migrations/2017_02_09_133117_add_categorie_type.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)