読み込みが終了しない場合は、しばらく待つか、リロードを行なってください。
If loading does not finish, wait for a while or reload.
エンジニア向けの情報を発信するブログです。
どなたでも発信できます。
お好きに利用していただれば幸いです。
@extends('layouts.app') @section('content') //↓ここから <form action="{{route('article.list')}}"> <button type="submit" name="sort" value="1">作成日順</button> <button type="submit" name="sort" value="">あいうえお順</button> </form> //↑ここまで追加 @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="{{route('article.edit', ['id' => $article->id])}}">編集</a> @endif </div> @endforeach @endsection
<button type="submit" name="sort" value="1">作成日順</button> <button type="submit" name="sort" value="">あいうえお順</button>
<input type="submit">
public function list(Request $request) { //↓ここから $sort = $request->get('sort'); if ($sort) { if ($sort === '1') { $articles = Article::orderBy('created_at')->get(); } } else { $articles = Article::all(); } //↑ここまで編集 return view( 'article.list', [ 'articles' => $articles ] ); }
if ($sort === '1') {
$articles = Article::orderBy('created_at')->get();
orderBy('created_at', 'ASC')
@extends('layouts.app') @section('content') <form action="{{route('article.list')}}"> <button type="submit" name="sort" value="1">作成日順</button> <button type="submit" name="sort" value="">あいうえお順</button> </form> @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> <small>{{$article->created_at}}</small>//<-追加 </div> @if (Auth::check() && Auth::user()->id === $article->user_id) <a href="{{route('article.edit', ['id' => $article->id])}}">編集</a> @endif </div> @endforeach @endsection
<small>{{$article->created_at}}</small>
public function list(Request $request) { $sort = $request->get('sort'); if ($sort) { if ($sort === '1') { $articles = Article::orderBy('created_at')->get(); //↓ここから } elseif ($sort === '2') { $articles = Article::orderBy('created_at', 'DESC')->get(); } //↑ここまで追加 } else { $articles = Article::all(); } return view( 'article.list', [ 'sort' => $sort,//<-追加 'articles' => $articles ] ); }
} elseif ($sort === '2') { $articles = Article::orderBy('created_at', 'DESC')->get(); }
return view( 'article.list', [ 'sort' => $sort, 'articles' => $articles ] );
<form action="{{route('article.list')}}"> <button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '1') 1 @elseif ($sort === '1') 2 @endif">作成日順</button> <button type="submit" name="sort" value="">あいうえお順</button> </form>
<button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '1') 1 @elseif ($sort === '1') 2 @endif">作成日順</button>
<form action="{{route('article.list')}}"> <button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '1') 1 @elseif ($sort === '1') 2 @endif">作成日順</button> <button type="submit" name="sort" value="3">あいうえお順</button>//<-編集 </form>
if ($sort) { if ($sort === '1') { $articles = Article::orderBy('created_at')->get(); } elseif ($sort === '2') { $articles = Article::orderBy('created_at', 'DESC')->get(); //↓追加 } elseif ($sort === '3') { $articles = Article::orderBy('title')->get(); } } else { $articles = Article::all(); }
} elseif ($sort === '3') { $articles = Article::orderBy('title')->get(); //↓追加 } elseif ($sort === '4') { $articles = Article::orderBy('title', 'DESC')->get(); }
<button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '3') 3 @elseif ($sort === '3') 4 @endif">あいうえお順</button>
//↓追加 <form action="#"> word: <input type="text" name="word"> <input type="submit" value="検索"> </form> //↑ <form action="{{route('article.list')}}"> <button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '1') 1 @elseif ($sort === '1') 2 @endif">作成日順</button> <button type="submit" name="sort" value="@if (!isset($sort) || $sort !== '3') 3 @elseif ($sort === '3') 4 @endif">あいうえお順</button> </form> //省略
<input type="text" name="word">
public function search(Request $request) { $word = $request->get('word'); if ($word !== null) { $escape_word = addcslashes($word, '\\_%'); $articles = Article::where('title', 'like', '%' . $escape_word . '%')->get(); } else { $articles = Article::all(); } return view( 'article.list', [ 'articles' => $articles ] ); }
$word = $request->get('word');
$escape_word = addcslashes($word, '\\_%');
$articles = Article::where('title', 'like', '%' . $escape_word . '%')->get();
'%' . $escape_word . '%'
Rotue::get('/article/list/search', 'ArticleController@search')->name('article.list.search');
<form action="{{route('article.list.search')}}">//<-編集 word: <input type="text" name="word"> <input type="submit" value="検索"> </form>