Skip to content

Commit f0d1c6b

Browse files
committed
태그 기능 구현
1 parent aacfbd5 commit f0d1c6b

29 files changed

+332
-112
lines changed

app/Http/Controllers/ArticlesController.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ public function __construct()
2121
*
2222
* @return \Illuminate\Http\Response
2323
*/
24-
public function index() {
25-
$articles = \App\Article::latest()->paginate(3);
24+
public function index($slug = null) {
25+
$query = $slug
26+
? \App\Tag::whereSlug($slug)->firstOrFail()->articles()
27+
: new \App\Article;
28+
29+
$articles = $query->latest()->paginate(3);
2630

2731
return view('articles.index', compact('articles'));
2832
}
@@ -54,6 +58,8 @@ public function store(\App\Http\Requests\ArticlesRequest $request) {
5458
return back()->withInput();
5559
}
5660

61+
$article->tags()->sync($request->input('tags'));
62+
5763
event(new \App\Events\ArticlesEvent($article));
5864
flash()->success('작성하신 글이 저장되었습니다.');
5965

@@ -80,7 +86,6 @@ public function show(\App\Article $article)
8086
public function edit(\App\Article $article)
8187
{
8288
$this->authorize('update', $article);
83-
flash()->success('수정하신 내용을 저장했습니다.');
8489

8590
return view('articles.edit', compact('article'));
8691
}
@@ -96,6 +101,7 @@ public function update(\App\Http\Requests\ArticlesRequest $request, \App\Article
96101
{
97102
$this->authorize('update', $article);
98103
$article->update($request->all());
104+
$article->tags()->sync($request->input('tags'));
99105
flash()->success('수정하신 내용을 저장했습니다.');
100106

101107
return redirect(route('articles.show', $article->id));

app/Http/Controllers/SessionsController.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,6 @@ public function store(Request $request)
5353
return $this->respondError('가입확인해 주세요.');
5454
}
5555

56-
if (\App\User::socialUser($request->input('email'))->first()) {
57-
return $this->respondError('소셜 로그인으로 로그인해주세요.');
58-
}
59-
6056
return $this->respondCreated(
6157
auth()->user()->name . '님, 환영합니다.'
6258
);

app/Http/Requests/ArticlesRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function rules()
2525
{
2626
return [
2727
'title' => ['required'],
28+
'tags' => ['required', 'array'],
2829
'content' => ['required', 'min:10'],
2930
];
3031
}

app/Providers/AppServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ class AppServiceProvider extends ServiceProvider
1313
*/
1414
public function boot()
1515
{
16-
//
16+
view()->composer('*', function ($view) {
17+
$allTags = \Cache::rememberForever('tags.list', function () {
18+
return \App\Tag::all();
19+
});
20+
21+
$view->with(compact('allTags'));
22+
});
1723
}
1824

1925
/**

app/Tag.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@
66

77
class Tag extends Model
88
{
9-
protected $fillable = ['name', 'slug'];
9+
/**
10+
* The attributes that are mass assignable.
11+
*
12+
* @var array
13+
*/
14+
protected $fillable = [
15+
'name',
16+
'slug',
17+
];
1018

1119
/* Relationships */
1220

1321
public function articles() {
1422
return $this->belongsToMany(Article::class);
1523
}
24+
25+
/* Accessors */
26+
27+
// public function getArticlesCountAttribute()
28+
// {
29+
// return $this->articles->count();
30+
// }
1631
}

config/project.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
<?php
22

33
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| 프로젝트 기본 정보
7+
|--------------------------------------------------------------------------
8+
*/
49
'url' => 'http://myapp.dev:8000',
10+
511
'description' => '',
12+
13+
/*
14+
|--------------------------------------------------------------------------
15+
| Tag 목록
16+
|--------------------------------------------------------------------------
17+
*/
18+
'tags' => [
19+
'laravel' => '라라벨',
20+
'lumen' => '루멘',
21+
'general' => '자유의견',
22+
'server' => '서버',
23+
'tip' => '',
24+
],
625
];

database/factories/ModelFactory.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
<?php
22

3-
/*
4-
|--------------------------------------------------------------------------
5-
| Model Factories
6-
|--------------------------------------------------------------------------
7-
|
8-
| Here you may define all of your model factories. Model factories give
9-
| you a convenient way to create models for testing and seeding your
10-
| database. Just tell the factory how a default model should look.
11-
|
12-
*/
13-
143
$factory->define(App\User::class, function (Faker\Generator $faker) {
154
$activated = $faker->randomElement([0, 1]);
165

@@ -26,10 +15,12 @@
2615

2716
$factory->define(App\Article::class, function (Faker\Generator $faker) {
2817
$date = $faker->dateTimeThisMonth;
18+
$userId = App\User::pluck('id')->toArray();
2919

3020
return [
3121
'title' => $faker->sentence(),
3222
'content' => $faker->paragraph(),
23+
'user_id' => $faker->randomElement($userId),
3324
'created_at' => $date,
3425
'updated_at' => $date,
3526
];

database/migrations/2016_09_10_011654_create_article_tag_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function down()
3939
$table->dropForeign('article_tag_tag_id_foreign');
4040
$table->dropForeign('article_tag_article_id_foreign');
4141
});
42-
42+
4343
Schema::dropIfExists('article_tag');
4444
}
4545
}

database/seeds/ArticlesTableSeeder.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ public function run()
1313
{
1414
$users = App\User::all();
1515

16-
$users->each(function ($user) {
17-
$user->articles()->save(
18-
factory(App\Article::class)->make()
19-
);
20-
});
16+
// $users->each(function ($user) {
17+
// $user->articles()->save(
18+
// factory(App\Article::class)->make()
19+
// );
20+
// });
21+
22+
App\Article::truncate();
23+
factory(App\Article::class, 50)->create();
2124
}
2225
}

database/seeds/DatabaseSeeder.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,48 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
$sqlite = in_array(config('database.default'), ['sqlite', 'testing']);
14+
$sqlite = in_array(config('database.default'), ['sqlite', 'testing'], true);
1515

1616
if (! $sqlite) {
1717
DB::statement('SET FOREIGN_KEY_CHECKS=0');
1818
}
1919

20-
App\User::truncate();
20+
/* 태그 */
21+
App\Tag::truncate();
22+
DB::table('article_tag')->truncate();
23+
$tags = config('project.tags');
24+
25+
foreach($tags as $slug => $name) {
26+
App\Tag::create([
27+
'name' => $name,
28+
'slug' => str_slug($slug) ]
29+
);
30+
}
31+
$this->command->info('Seeded: tags table');
32+
33+
/* User */
2134
$this->call(UsersTableSeeder::class);
2235

23-
App\Article::truncate();
36+
/* 아티클 */
2437
$this->call(ArticlesTableSeeder::class);
2538

39+
// 변수 선언
40+
$faker = app(Faker\Generator::class);
41+
$users = App\User::all();
42+
$articles = App\Article::all();
43+
$tags = App\Tag::all();
44+
45+
// 아티클과 태그 연결
46+
foreach($articles as $article) {
47+
$article->tags()->sync(
48+
$faker->randomElements(
49+
$tags->pluck('id')->toArray(),
50+
rand(1, 3)
51+
)
52+
);
53+
}
54+
$this->command->info('Seeded: article_tag table');
55+
2656
if (! $sqlite) {
2757
DB::statement('SET FOREIGN_KEY_CHECKS=1');
2858
}

0 commit comments

Comments
 (0)