読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。

変更履歴
2021/09/12 validationのリダイレクト処理を編集しました。











php artisan make:migration create_tags_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');//<- 追加
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
php artisan migrate

php artisan make:migration create_article_tag_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticleTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('article_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('article_id');//<-追加
$table->unsignedBigInteger('tag_id');//<-追加
$table->timestamps();
//↓諸々追加
$table->foreign('article_id')
->references('id')
->on('articles')
->onDelete('cascade');
$table->foreign('tag_id')
->references('id')
->on('tags')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('article_tag');
}
}

php artisan make:model Tag
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = [
'name',
];
}
public function tags()
{
return $this->belongsToMany('App\Tag')->withTimestamps();
}
belongsToMany
withTimestamps()
belongsToMany('App\Article')
@extends('layouts.app')
@section('content')
<form action="{{route('article.create')}}" method="post">
@csrf
<input name="title" type="text" value="{{old('title')}}">
<input type="text" name="tag" value="{{old('tag')}}">//<-追加
<textarea name="content" cols="30" rows="10">{{old('content')}}</textarea>
<input type="submit">
</form>
@endsection



~~省略
use App\Tag;//<-追加
~~省略
public function create(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => [
'required',
'string',
'max:25',
],
'content' => [
'required',
'string',
'max:4000',
],
//↓追加
'tag' => [
'nullable',
'string',
],
]);
$validator->validate();
$title = $request->get('title');
$content = $request->get('content');
$user_id = Auth::id();
$article = Article::create(
[
'title' => $title,
'content' => $content,
'user_id' => $user_id,
]
);
//↓諸々追加
$input_tag = $request->get('tag');
if (isset($input_tag)) {
$tag_ids = [];
$tags = explode(',', $input_tag);
foreach ($tags as $tag) {
$tag = Tag::updateOrCreate(
[
'name' => $tag,
]
);
$tag_ids[] = $tag->id;
}
$article->tags()->sync($tag_ids);
}
return redirect()->route('article.show', ['id' => $article->id]);
}
'tag' => [
'nullable',
'string',
],
$input_tag = $request->get('tag');
if (isset($input_tag)) {
$tag_ids = [];
$tags = explode(',', $input_tag);
foreach ($tags as $tag) {
$tag = Tag::updateOrCreate(
[
'name' => $tag,
]
);
$tag_ids[] = $tag->id;
}
$article->tags()->sync($tag_ids);
}
$input_tag = $request->get('tag');
if (isset($input_tag)) {
$tag_ids = [];
$tags = explode(',', $input_tag);
foreach ($tags as $tag) {
$tag = Tag::updateOrCreate(
[
'name' => $tag,
]
);
$tag_ids[] = $tag->id;
$article->tags()->sync($tag_ids);



@extends('layouts.app')
@section('content')
投稿者: {{$article->user->name}}
<br>
title: {{$article->title}}
<br>
//↓色々追加
tag:
@if ($article->tags()->exists())
@foreach ($article->tags as $tag)
<span style="margin-left: 5px;">{{$tag->name}}</span>
@endforeach
@else
タグの登録はありません。
@endif
<br>
内容:
<br>
{!!nl2br(e($article->content))!!}
@endsection
@if
@if ($article->tags()->exists())
@foreach ($article->tags as $tag)
<span style="margin-left: 5px;">{{$tag->name}}</span>
@endforeach
@else

