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

質問編集履歴

5

タイトルに追記

2020/07/14 02:18

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
@@ -1,1 +1,1 @@
1
- データをviewで表示できない 【タグ機能】【中間テーブル】Trying to get property 'tag' of non-object (View:
1
+ 【タグ機能】データをviewで表示できない 【中間テーブル】Trying to get property 'tag' of non-object (View:
body CHANGED
File without changes

4

書式改善

2020/07/14 02:18

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
@@ -1,1 +1,1 @@
1
- foreach内でデータベース参照できない
1
+ データをview表示できない 【タグ機能】【中間テーブル】Trying to get property 'tag' of non-object (View:
body CHANGED
File without changes

3

書式改善

2020/07/14 02:17

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,4 @@
1
- **質問**
1
+ #**質問**
2
2
  tagsテーブルのtagに入っているデータをforeachで表示させようとしたのですが、`Trying to get property 'tag' of non-object (View: `とエラーが出てしまい出力されません
3
3
 
4
4
  ```
@@ -8,7 +8,7 @@
8
8
  ```
9
9
 
10
10
 
11
- **試してみたこと**
11
+ #**試してみたこと**
12
12
  tagsテーブルのtagにはデータは入っています。
13
13
  他テーブルからはこのforeach内で出力できているのですが、なぜでしょうか?
14
14
  var_dumpで{{$item}}はtag関連以外のデータは確認できました。
@@ -16,7 +16,7 @@
16
16
  dd($item->tag) も NULL
17
17
  dd($item->tags)もNULLでした。
18
18
 
19
- **怪しいところ**
19
+ #**怪しいところ**
20
20
  tag_title という中間テーブルを使い title と tag を紐付けているのですが、
21
21
  リレーションが間違っている可能性があるので追記します。
22
22
 

2

タイトルに追記

2020/07/14 02:03

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,4 @@
1
+ **質問**
1
2
  tagsテーブルのtagに入っているデータをforeachで表示させようとしたのですが、`Trying to get property 'tag' of non-object (View: `とエラーが出てしまい出力されません
2
3
 
3
4
  ```

1

中間テーブル周りに怪しい部分が出てきたので追記

2020/07/14 02:00

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -5,5 +5,37 @@
5
5
  <td>{{$item->tag->tag}}</td>
6
6
  @endforeach
7
7
  ```
8
+
9
+
10
+ **試してみたこと**
8
11
  tagsテーブルのtagにはデータは入っています。
9
- 他テーブルからはこのforeach内で出力できているのですが、なぜでしょうか?
12
+ 他テーブルからはこのforeach内で出力できているのですが、なぜでしょうか?
13
+ var_dumpで{{$item}}はtag関連以外のデータは確認できました。
14
+ var_dumpで{{$item->tag->tag}}を確認したところNULL
15
+ dd($item->tag) も NULL
16
+ dd($item->tags)もNULLでした。
17
+
18
+ **怪しいところ**
19
+ tag_title という中間テーブルを使い title と tag を紐付けているのですが、
20
+ リレーションが間違っている可能性があるので追記します。
21
+
22
+ Tag.php
23
+ ```
24
+ public function titles()
25
+ {
26
+ return $this->belongsToMany('App\title');
27
+ }
28
+ ```
29
+
30
+ Title.php
31
+ ```
32
+ public function tags()
33
+ {
34
+ return $this->belongsToMany('App\Tag');
35
+ }
36
+
37
+ ```
38
+
39
+ そして中間テーブルには
40
+ tag_id
41
+ title_idのカラムが作られていて、登録時にtitleのIDとtag_idが渡されています。