読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。
変更履歴
2021/09/12 validationのリダイレクト処理を編集しました。
Route::get('/article/list', 'ArticleController@list')->name('article.list');
//省略 public function list(Request $request) { $articles = Article::all(); return view( 'article.list', [ 'articles' => $articles ] ); }
$articles = Article::all();
@extends('layouts.app') @section('content') @foreach ($articles as $article) <div style="border-bottom: solid 1px gray;"> <div> user: {{$article->user->name}} </div> <div> title: {{$article->title}} </div> </div> @endforeach @endsection
//省略 <a href="{{route('article.new')}}">記事投稿</a> <a href="{{route('article.list')}}">ブログ一覧</a>//<-追加 //省略
@extends('layouts.app') @section('content') @foreach ($articles as $article) <div style="border-bottom: solid 1px gray;"> <div> user: {{$article->user->name}} </div> <div> title: <a href="{{route('article.show', ['id' => $article->id])}}">{{$article->title}}</a>//<-編集 </div> </div> @endforeach @endsection
@extends('layouts.app') @section('content') @foreach ($articles as $article) <div style="border-bottom: solid 1px gray;"> <div> user: {{$article->user->name}} </div> <div> title: <a href="{{route('article.show', ['id' => $article->id])}}">{{$article->title}}</a> </div> ↓追加 @if (Auth::check() && Auth::user()->id === $article->user_id) <a href="#">編集</a> @endif </div> @endforeach @endsection
Auth::check()
&&
Auth::user()->id
===
$article->user_id
Route::get('/article/edit/{id}', 'ArticleController@edit')->name('article.edit')->middleware('auth');
public function edit(Request $request) { $id = $request->route('id'); $article = Article::find($id); if (!isset($article) || $article->user_id !== Auth::id()) { abort(404); } $title = $article->title; $content = $article->content; $tags = $article->tags->pluck('name')->toArray(); return view( 'article.new', [ 'id' => $id, 'title' => $title, 'content' => $content, 'tag' => !empty($tags) ? implode(',', $tags) : null, ] ); }
if (!isset($article) || $article->user_id !== Auth::id()) {
$title = $article->title; $content = $article->content; $tags = $article->tags->pluck('name')->toArray();
$tags = $article->tags->pluck('name')->toArray();
$article->tags
->pluck('name')
->toArray();
return view( 'article.new', [ 'id' => $id, 'title' => $title, 'content' => $content, 'tag' => !empty($tags) ? implode(',', $tags) : null, ] );
'tag' => !empty($tags) ? implode(',', $tags) : null,
'article.new',
@extends('layouts.app') @section('content') <form action="{{route('article.create')}}" method="post"> @csrf <input type="hidden" name="id" value="{{$id ?? null}}">//<-追加 <input name="title" type="text" value="{{old('title', $title ?? null)}}">//<-編集 <input type="text" name="tag" value="{{old('tag', $tag ?? null)}}">//<-編集 <textarea name="content" cols="30" rows="10">{{old('content', $content ?? null)}}</textarea>//<-編集 <input type="submit"> </form> @endsection
<input type="hidden" name="id" value="{{$id ?? null}}">
{{old('title', $title ?? null)}}
$title ?? null
isset($title) ? $title : null
//省略 @if (Auth::check() && Auth::user()->id === $article->user_id) <a href="{{route('article.edit', ['id' => $article->id])}}">編集</a>//<-編集 @endif //省略
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(); //↓ここから $id = $request->get('id'); $article = Article::find($id); if (isset($article) && $article->user_id !== Auth::user()->id) { abort(404); } //↑ここまで追加 $title = $request->get('title'); $content = $request->get('content'); ↓ここから if (isset($article)) { $article->title = $title; $article->content = $content; $article->save(); } else { $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]); }
$id = $request->get('id'); $article = Article::find($id); if (isset($article) && $article->user_id !== Auth::user()->id) { abort(404); }
if (isset($article)) { $article->title = $title; $article->content = $content; $article->save(); } else { $user_id = Auth::id(); $article = Article::create( [ 'title' => $title, 'content' => $content, 'user_id' => $user_id, ] ); }
$article->title = $title; $article->content = $content;
$article->title
$article->title = $title;
$article->save();
$user_id = Auth::id();
public function delete(Request $request) { $id = $request->get('id'); $article = Article::find($id); if (!isset($article) || $article->user_id !== Auth::id()) { abort(404); } $article->delete(); $articles = Article::all(); return view( 'article.list', [ 'articles' => $articles, ] ); }
$article->delete();
Route::post('/article/delete', 'ArticleController@delete')->name('article.delete')->middleware('auth');
//省略 @if (Auth::check() && Auth::user()->id === $article->user_id) <a href="{{route('article.edit', ['id' => $article->id])}}">編集</a> //↓ここから <form action="{{route('article.delete')}}" method="post"> @csrf <button type="submit" name="id" value="{{$article->id}}"> 削除 </button> </form> //↑ここまで追加 @endif //省略