Add colors to tags
MODIFY
app/Http/Controllers/TagController.php
app/Http/Controllers/TagController.php
@@ -27,7 +27,8 @@ class TagController extends Controller {
Tag::create([
'space_id' => session('space')->id,
- 'name' => $request->input('name')
+ 'name' => $request->input('name'),
+ 'color' => Tag::randomColor()
]);
return redirect()->route('tags.index');

MODIFY
app/Tag.php
app/Tag.php
@@ -12,14 +12,24 @@ class Tag extends Model {
protected $dates = ['deleted_at'];
- protected $fillable = ['space_id', 'name'];
+ protected $fillable = ['space_id', 'name', 'color'];
protected $dispatchesEvents = [
'created' => TagCreated::class,
'deleted' => TagDeleted::class
];
+ // Relations
public function spendings() {
return $this->hasMany(Spending::class);
}
+
+ // Custom
+ private static function randomColorPart() {
+ return str_pad(dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);
+ }
+
+ public static function randomColor() {
+ return self::randomColorPart() . self::randomColorPart() . self::randomColorPart();
+ }
}

ADD
database/migrations/2018_12_21_222756_add_color_to_tags_table.php
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class AddColorToTagsTable extends Migration {
+ public function up() {
+ Schema::table('tags', function (Blueprint $table) {
+ $table->char('color', 6)->after('name');
+ });
+ }
+
+ public function down() {
+ Schema::table('tags', function (Blueprint $table) {
+ $table->dropColumn('color');
+ });
+ }
+}

MODIFY
resources/views/tags/index.blade.php
resources/views/tags/index.blade.php
<div class="box">
@if (count($tags))
<div class="box__section box__section--header row">
+ <div class="row__column row__column--compact mr-2" style="width: 20px;"></div>
<div class="row__column">{{ __('fields.name') }}</div>
<div class="row__column row__column--double" style="flex: 2;">{{ __('models.spendings') }}</div>
</div>
@foreach ($tags as $tag)
<div class="box__section row">
- <div class="row__column">{{ $tag->name }}</div>
- <div class="row__column">{{ $tag->spendings->count() }}</div>
+ <div class="row__column row__column--compact row__column--middle mr-2">
+ <div style="width: 15px; height: 15px; border-radius: 2px; background: #{{ $tag->color }};"></div>
+ </div>
+ <div class="row__column row__column--middle">{{ $tag->name }}</div>
+ <div class="row__column row__column--middle">{{ $tag->spendings->count() }}</div>
<div class="row__column row__column--middle row row--right">
<div class="row__column row__column--compact">
<a href="/tags/{{ $tag->id }}/edit">

Add colors to tags