タグ登録機能の追加

This commit is contained in:
shibafu 2018-01-08 08:50:22 +09:00
parent d7b16cd6d5
commit b1dcc36565
7 changed files with 96 additions and 12 deletions

View File

@ -22,4 +22,9 @@ class Ejaculation extends Model
{ {
return $this->belongsTo('App\User'); return $this->belongsTo('App\User');
} }
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Tag;
use App\User; use App\User;
use Carbon\Carbon; use Carbon\Carbon;
use Validator; use Validator;
@ -28,6 +29,7 @@ class EjaculationController extends Controller
'time' => 'required|date_format:H:i', 'time' => 'required|date_format:H:i',
'note' => 'nullable|string|max:500', 'note' => 'nullable|string|max:500',
'link' => 'nullable|url', 'link' => 'nullable|url',
'tags' => 'nullable|string',
])->after(function ($validator) use ($request, $inputs) { ])->after(function ($validator) use ($request, $inputs) {
// 日時の重複チェック // 日時の重複チェック
if (!$validator->errors()->hasAny(['date', 'time'])) { if (!$validator->errors()->hasAny(['date', 'time'])) {
@ -46,6 +48,15 @@ class EjaculationController extends Controller
'is_private' => $request->has('is_private') ?? false 'is_private' => $request->has('is_private') ?? false
]); ]);
$tags = explode(' ', $inputs['tags']);
$tagIds = [];
foreach ($tags as $tag) {
$tag = Tag::firstOrCreate(['name' => $tag]);
$tagIds[] = $tag->id;
}
$ejaculation->tags()->sync($tagIds);
return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!'); return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!');
} }
@ -115,6 +126,7 @@ class EjaculationController extends Controller
{ {
$ejaculation = Ejaculation::findOrFail($id); $ejaculation = Ejaculation::findOrFail($id);
$user = User::findOrFail($ejaculation->user_id); $user = User::findOrFail($ejaculation->user_id);
$ejaculation->tags()->detach();
$ejaculation->delete(); $ejaculation->delete();
return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。'); return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。');
} }

19
app/Tag.php Normal file
View File

@ -0,0 +1,19 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
//
protected $fillable = [
'name'
];
public function ejaculations()
{
return $this->belongsToMany('App\Ejaculation')->withTimestamps();
}
}

View File

@ -0,0 +1,40 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('ejaculation_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('ejaculation_id')->index();
$table->integer('tag_id')->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
Schema::dropIfExists('ejaculation_tag');
}
}

View File

@ -36,7 +36,7 @@
</div> </div>
<div class="form-row"> <div class="form-row">
<div class="form-group col-sm-12"> <div class="form-group col-sm-12">
<input name="tags" type="hidden"> <input name="tags" type="hidden" value="{{ old('tags') }}">
<label for="tagInput"><span class="oi oi-tags"></span> タグ</label> <label for="tagInput"><span class="oi oi-tags"></span> タグ</label>
<div class="form-control {{ $errors->has('tags') ? ' is-invalid' : '' }}"> <div class="form-control {{ $errors->has('tags') ? ' is-invalid' : '' }}">
<ul id="tags" class="list-inline d-inline"></ul> <ul id="tags" class="list-inline d-inline"></ul>
@ -112,6 +112,16 @@
.join(' ') .join(' ')
); );
} }
function insertTag(value) {
$('#tags').append('<li class="list-inline-item badge badge-primary" style="cursor: pointer;" data-value="' + value + '"><span class="oi oi-tag"></span> ' + value + ' | x</li>');
}
var initTags = $('input[name=tags]').val();
if (initTags.trim() !== '') {
initTags.split(' ').forEach(function (value) {
insertTag(value);
});
}
$('#tagInput').on('keydown', function (ev) { $('#tagInput').on('keydown', function (ev) {
var $this = $(this); var $this = $(this);
if ($this.val().trim() !== '') { if ($this.val().trim() !== '') {
@ -119,7 +129,7 @@
case 'Tab': case 'Tab':
case 'Enter': case 'Enter':
case ' ': case ' ':
$('#tags').append('<li class="list-inline-item badge badge-primary" style="cursor: pointer;" data-value="' + $this.val().trim() + '">' + $this.val().trim() + ' | x</li>'); insertTag($this.val().trim());
$this.val(''); $this.val('');
updateTags(); updateTags();
ev.preventDefault(); ev.preventDefault();

View File

@ -34,15 +34,14 @@
@endif @endif
</div> </div>
<!-- tags --> <!-- tags -->
@if ($ejaculation->is_private) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}} @if ($ejaculation->is_private || $ejaculation->tags->isNotEmpty())
<p class="mb-2"> <p class="mb-2">
@if ($ejaculation->is_private) @if ($ejaculation->is_private)
<span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span> <span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span>
@endif @endif
{{-- @foreach ($ejaculation->tags as $tag)
<span class="badge badge-secondary"><span class="oi oi-tag"></span> 催眠音声</span> <span class="badge badge-secondary"><span class="oi oi-tag"></span> {{ $tag->name }}</span>
<span class="badge badge-secondary"><span class="oi oi-tag"></span> 適当なタグ</span> @endforeach
--}}
</p> </p>
@endif @endif
<!-- okazu link --> <!-- okazu link -->

View File

@ -20,15 +20,14 @@
@endif @endif
</div> </div>
<!-- tags --> <!-- tags -->
@if ($ejaculation->is_private) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}} @if ($ejaculation->is_private || $ejaculation->tags->isNotEmpty())
<p class="mb-2"> <p class="mb-2">
@if ($ejaculation->is_private) @if ($ejaculation->is_private)
<span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span> <span class="badge badge-warning"><span class="oi oi-lock-locked"></span> 非公開</span>
@endif @endif
{{-- @foreach ($ejaculation->tags as $tag)
<span class="badge badge-secondary"><span class="oi oi-tag"></span> 催眠音声</span> <span class="badge badge-secondary"><span class="oi oi-tag"></span> {{ $tag->name }}</span>
<span class="badge badge-secondary"><span class="oi oi-tag"></span> 適当なタグ</span> @endforeach
--}}
</p> </p>
@endif @endif
<!-- okazu link --> <!-- okazu link -->