質問するログイン新規登録

質問編集履歴

2

訂正

2021/03/03 08:03

投稿

YukiTani
YukiTani

スコア19

title CHANGED
File without changes
body CHANGED
@@ -67,6 +67,26 @@
67
67
 
68
68
  ```
69
69
 
70
+ ```php
71
+ この書き方でも何かが駄目で、上手く動かない。
72
+ /**
73
+ * 投稿一覧表示アクション
74
+ */
75
+ public function index(Request $request)
76
+ {
77
+ if ($request->filled('keyword')) {
78
+ $keyword = $request->input('keyword');
79
+ $content = '検索キーワード: '.$keyword;
80
+ $microposts = Micropost::where('content', 'like', '%'.$keyword.'%')->get();
81
+ }else{
82
+ $content = "検索キーワードを入力してください。";
83
+ $microposts = Micropost::all();
84
+ }
85
+ return view('micropost.index', ['content' => $content, 'microposts' => $microposts]);
86
+ }
87
+ ```
88
+
89
+
70
90
  ### 試したこと
71
91
 
72
92
  投稿一覧表示アクション内に検索実装を追加しました。

1

訂正

2021/03/03 08:03

投稿

YukiTani
YukiTani

スコア19

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- 開発環境で、投稿アプリの投稿のキーワードを拾い、表示させる検索処理を実装しています。
3
+ 開発環境で、投稿アプリの投稿のキーワード(content/users)を拾い、表示させる検索処理を実装しています。
4
4
 
5
5
  ### 発生している問題
6
6
  検索処理が行われない。
@@ -21,7 +21,7 @@
21
21
  $viewParams = [
22
22
  'microposts' => $microposts,
23
23
  ];
24
- return view('micropost.index', $viewParams);
24
+
25
25
 
26
26
  #キーワード受け取り
27
27
  $search = $request->input('search');
@@ -30,7 +30,9 @@
30
30
  #もしキーワードがあったら
31
31
  if($search = request('search'))
32
32
  {
33
- $query->where('name','like','%{$search}%')->orWhere('content','like','%{$search}%');
33
+ $query->where('users','like','%{$search}%')->orWhere('content','like','%{$search}%');
34
+
35
+       return view('micropost.index', $viewParams);
34
36
  }
35
37
 
36
38
 
@@ -43,6 +45,26 @@
43
45
  <input type="submit" value="検索" class="btn btn-info">
44
46
  </form>
45
47
 
48
+ my-laravel-app/database/migrations/2021_02_26_141741_create_microposts_table.php
49
+
50
+ class CreateMicropostsTable extends Migration
51
+ {
52
+ /**
53
+ * Run the migrations.
54
+ *
55
+ * @return void
56
+ */
57
+ public function up()
58
+ {
59
+ Schema::create('microposts', function (Blueprint $table) {
60
+ $table->bigIncrements('id');
61
+ $table->unsignedBigInteger('user_id');
62
+ $table->foreign('user_id')->references('id')->on('users');
63
+ $table->text('content');
64
+ $table->timestamps();
65
+ });
66
+ }
67
+
46
68
  ```
47
69
 
48
70
  ### 試したこと