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

質問編集履歴

7

2020/04/29 16:04

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -11,12 +11,6 @@
11
11
  $post->tags()->attach($tags_id);
12
12
  「Call to a member function tags() on array」
13
13
 
14
- 再修正
15
- -----------------------
16
- $post = new Post;//追加
17
- //エラー
18
- SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into `post_tag` (`created_at`, `post_id`, `tag_id`, `updated_at`) values (2020-04-29 21:59:21, ?, 1, 2020-04-29 21:59:21)
19
-
20
14
  TOXI法によるタグ付け機能を考えています
21
15
  テーブルはこのような感じです
22
16
  **posts**

6

修正

2020/04/29 16:04

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,16 @@
7
7
  解決方法をご教示くださいよろしくお願いします。
8
8
  【参考サイト】
9
9
  [https://qiita.com/AkiYanagimoto/items/b363d673d9f2bf63fc0f](https://qiita.com/AkiYanagimoto/items/b363d673d9f2bf63fc0f)
10
+ 修正後------------------------------------
11
+ $post->tags()->attach($tags_id);
12
+ 「Call to a member function tags() on array」
10
13
 
14
+ 再修正
15
+ -----------------------
16
+ $post = new Post;//追加
17
+ //エラー
18
+ SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'post_id' cannot be null (SQL: insert into `post_tag` (`created_at`, `post_id`, `tag_id`, `updated_at`) values (2020-04-29 21:59:21, ?, 1, 2020-04-29 21:59:21)
19
+
11
20
  TOXI法によるタグ付け機能を考えています
12
21
  テーブルはこのような感じです
13
22
  **posts**
@@ -25,12 +34,7 @@
25
34
  |:--|:--:|
26
35
  ||||
27
36
 
28
- 修正後------------------------------------
29
- $post->tags()->attach($tags_id);
30
- 「Call to a member function tags() on array」
31
37
 
32
-
33
-
34
38
  ```ここに言語を入力
35
39
  #PostsController.php
36
40
  <?php
@@ -45,7 +49,8 @@
45
49
 
46
50
  public function store(Request $request)
47
51
  {
52
+ $post = new Post;//追加
48
- $post = $request->validate([ //修正
53
+ $post = $request->validate([
49
54
  'title' => 'required|max:50',
50
55
  'body' => 'required|max:2000',
51
56
  ]);

5

修正

2020/04/29 13:07

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  このエラーが起こる原因が掴めません
7
7
  解決方法をご教示くださいよろしくお願いします。
8
+ 【参考サイト】
9
+ [https://qiita.com/AkiYanagimoto/items/b363d673d9f2bf63fc0f](https://qiita.com/AkiYanagimoto/items/b363d673d9f2bf63fc0f)
8
10
 
9
11
  TOXI法によるタグ付け機能を考えています
10
12
  テーブルはこのような感じです

4

修正

2020/04/29 07:36

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -26,9 +26,9 @@
26
26
  修正後------------------------------------
27
27
  $post->tags()->attach($tags_id);
28
28
  「Call to a member function tags() on array」
29
- -------------------------------------------
30
29
 
31
30
 
31
+
32
32
  ```ここに言語を入力
33
33
  #PostsController.php
34
34
  <?php

3

修正

2020/04/29 05:50

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -3,10 +3,26 @@
3
3
  「Method Illuminate\Http\Request::tags does not exist.」
4
4
  ↑の部分でエラーがでて困っています。
5
5
 
6
-
7
6
  このエラーが起こる原因が掴めません
8
7
  解決方法をご教示くださいよろしくお願いします。
9
8
 
9
+ TOXI法によるタグ付け機能を考えています
10
+ テーブルはこのような感じです
11
+ **posts**
12
+ |id|title|body|
13
+ |:--|:--:|--:|
14
+ ||||
15
+
16
+ **post_tag**
17
+ |id|post_id|tag_id|
18
+ |:--|:--:|--:|
19
+ ||||
20
+
21
+ **tags**
22
+ |id|tag_name|
23
+ |:--|:--:|
24
+ ||||
25
+
10
26
  修正後------------------------------------
11
27
  $post->tags()->attach($tags_id);
12
28
  「Call to a member function tags() on array」

2

修正

2020/04/29 05:49

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,13 @@
7
7
  このエラーが起こる原因が掴めません
8
8
  解決方法をご教示くださいよろしくお願いします。
9
9
 
10
+ 修正後------------------------------------
11
+ $post->tags()->attach($tags_id);
12
+ 「Call to a member function tags() on array」
13
+ -------------------------------------------
14
+
15
+
10
- ```
16
+ ```ここに言語を入力
11
17
  #PostsController.php
12
18
  <?php
13
19
  namespace App\Http\Controllers;
@@ -21,6 +27,11 @@
21
27
 
22
28
  public function store(Request $request)
23
29
  {
30
+ $post = $request->validate([ //修正
31
+ 'title' => 'required|max:50',
32
+ 'body' => 'required|max:2000',
33
+ ]);
34
+
24
35
  preg_match_all('/#([a-zA-z0-90-9ぁ-んァ-ヶ亜-熙]+)/u', $request->tags, $match);
25
36
       
26
37
  $tags = [];
@@ -33,25 +44,47 @@
33
44
  array_push($tags_id, $tag['id']);
34
45
  }
35
46
 
36
- $post = $request;
47
+
37
48
  $post->tags()->attach($tags_id);//エラー箇所
38
49
  //...
39
50
  }
40
51
  //...
41
52
  }
53
+
42
54
  ```
55
+
43
- ```ここに言語を入力
56
+ ```
44
- # Post.php
57
+ #Post.php
58
+ <?php
59
+ namespace App;
60
+ use Illuminate\Database\Eloquent\Model;
61
+ class Post extends Model
62
+ {
63
+ protected $fillable = [
64
+ 'title',
65
+ 'body',
66
+ ];
45
- public function tags()
67
+ public function tags()
46
68
  {
47
- return $this->belongsToMany('App\Tag');
69
+ return $this->belongsToMany('App\Tag','post_tag');//修正
48
70
  }
49
71
 
72
+ {
73
+
50
74
  # Tag.php
75
+ <?php
76
+ namespace App;
77
+ use Illuminate\Database\Eloquent\Model;
78
+ class Tag extends Model
79
+ {
80
+ protected $fillable = [
81
+ 'tag_name',
82
+ ];
51
- public function posts()
83
+ public function posts()
52
- {
84
+ {
53
- return $this->belongsToMany('App\Post');
85
+ return $this->belongsToMany('App\Post','post_tag');//修正
54
- }
86
+ }
87
+ }
55
88
  ```
56
89
  ```
57
90
  #web.php

1

誤字

2020/04/29 05:34

投稿

rab96
rab96

スコア4

title CHANGED
File without changes
body CHANGED
@@ -33,7 +33,7 @@
33
33
  array_push($tags_id, $tag['id']);
34
34
  }
35
35
 
36
- $post = $request
36
+ $post = $request;
37
37
  $post->tags()->attach($tags_id);//エラー箇所
38
38
  //...
39
39
  }