Skip to content

Commit

Permalink
Separate category name and slug
Browse files Browse the repository at this point in the history
  • Loading branch information
kutaloweb committed Aug 5, 2018
1 parent 03a3950 commit 5764b02
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
12 changes: 2 additions & 10 deletions app/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
* @property int $id
* @property string $name
* @property string $slug
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @property-read string $detail
Expand All @@ -23,18 +24,9 @@ class Category extends Model
*/
protected $fillable = [
'name',
'slug'
];

/**
* Get the category name.
*
* @return string
*/
public function getDetailAttribute()
{
return ucfirst($this->name);
}

/**
* Get the category posts.
*
Expand Down
5 changes: 4 additions & 1 deletion app/Console/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ protected function createCategories()
$categories = $this->category->listName();
foreach (config('system.default_category') as $key => $value) {
if (!in_array($value, $categories)) {
Category::create(['name' => $value]);
Category::create([
'name' => $value,
'slug' => str_slug($value),
]);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ public function index()
*/
public function store(CategoryRequest $request)
{
if (Category::whereName(str_slug(request('name')))->exists()) {
if (Category::where('slug', str_slug(request('name')))->exists()) {
return $this->error(['message' => trans('category.exists')]);
}
$category = Category::create(['name' => str_slug(request('name'))]);
$category = Category::create([
'name' => request('name'),
'slug' => str_slug(request('name')),
]);

$this->activity->record([
'module' => $this->module,
Expand Down
2 changes: 1 addition & 1 deletion app/Repositories/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getBySlug($slug)
*/
public function getByCategoryAndSlug($category, $slug)
{
$category = Category::where('name', $category)->first();
$category = Category::where('slug', $category)->first();

if (!$category) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"user" => "user",
],
"default_category" => [
"articles" => "articles",
"articles" => "Articles",
],
"public_config" => [
"color_theme",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddSlugColumnToCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->string('slug')->nullable()->after('name');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('categories', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
}
2 changes: 1 addition & 1 deletion resources/assets/js/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ let routes = [
{
path: '/:category/:slug',
component: require('./views/post/View.vue'),
meta: {title: appName + ' | ' + i18n.post.view}
meta: {title: appName}
},
{
path: '*',
Expand Down
7 changes: 3 additions & 4 deletions resources/assets/js/views/category/Category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
<thead>
<tr>
<th>{{ trans('category.name') }}</th>
<th>{{ trans('category.slug') }}</th>
<th class="table-option">Action</th>
</tr>
</thead>
<tbody>
<tr v-for="category in categories.data">
<td v-text="toWord(category.name)"></td>
<td v-text="category.name"></td>
<td v-text="category.slug"></td>
<td class="table-option">
<div class="btn-group">
<button class="btn btn-danger btn-sm" :key="category.id"
Expand Down Expand Up @@ -122,9 +124,6 @@
.catch(error => {
helper.showDataErrorMsg(error);
});
},
toWord(str) {
return helper.toWord(str);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions resources/assets/js/views/post/PostCard.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<router-link class="card" :to="`${post.category.name}/${post.slug}`">
<router-link class="card" :to="`${post.category.slug}/${post.slug}`">
<div class="card-img"
:class="[post.cover !== 'uploads/images/cover-default.png' ? 'cover-img' : '']">
<img class="card-img-top img-responsive" :src="post.cover" :alt="post.title">
Expand All @@ -9,7 +9,7 @@
<h5 class="card-text">{{ limitWords(post.stripped_body, 35) }}</h5>
<p class="card-text">
<small class="text-muted card-caps">
{{ toWord(post.category.name) }} / {{ post.created_at }}
{{ post.category.name }} / {{ post.created_at }}
</small>
</p>
</div>
Expand All @@ -36,9 +36,6 @@
}
return textToLimit;
},
toWord(str) {
return helper.toWord(str);
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions resources/assets/js/views/post/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="col-9 col-md-9">
<h1 class="card-title post-title">{{ post.title }}</h1>
<span class="text-muted card-caps">
{{ toWord(categoryName) }} / {{ post.created_at }}
{{ categoryName }} / {{ post.created_at }}
</span>
<hr>
<div class="card-text" v-html="post.body"></div>
Expand Down Expand Up @@ -65,11 +65,6 @@
.catch(error => {
helper.showDataErrorMsg(error);
});
},
methods: {
toWord(str) {
return helper.toWord(str);
}
}
}
</script>

0 comments on commit 5764b02

Please sign in to comment.