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

質問編集履歴

4

タイトル変更

2020/10/13 06:47

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
@@ -1,1 +1,1 @@
1
- 中間テーブルのデータをviewで表示できない
1
+ 多対多のリレーション先テーブルのをviewで表示できない
body CHANGED
File without changes

3

タイトル変更

2020/10/13 06:47

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
@@ -1,1 +1,1 @@
1
- 中間テーブルのをviewで取得した
1
+ 中間テーブルのデータをviewで表示できな
body CHANGED
File without changes

2

モデルを追記

2020/10/13 06:43

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -60,6 +60,55 @@
60
60
  </tr>
61
61
  ```
62
62
 
63
+ ```ここに言語を入力
64
+ Store.php
65
+ <?php
66
+
67
+ namespace App\Models;
68
+
69
+ use Illuminate\Database\Eloquent\Factories\HasFactory;
70
+ use Illuminate\Database\Eloquent\Model;
71
+
72
+ class Store extends Model
73
+ {
74
+ use HasFactory;
75
+ protected $table = 'stores';
76
+
77
+ // 店舗に所属する連携サイトを取得
78
+ public function sites()
79
+ {
80
+ return $this->belongsToMany('App\Models\Site');
81
+ }
82
+ }
83
+
84
+ ```
85
+
86
+ ```ここに言語を入力
87
+ site.php
88
+
89
+ <?php
90
+
91
+ namespace App\Models;
92
+
93
+ use Illuminate\Database\Eloquent\Factories\HasFactory;
94
+ use Illuminate\Database\Eloquent\Model;
95
+
96
+ class Site extends Model
97
+ {
98
+ use HasFactory;
99
+ protected $table = 'sites';
100
+ protected $fillable = ['site_name', 'site_url'];
101
+
102
+ public function stores()
103
+ {
104
+ return $this->belongsToMany('App\Models\Store');
105
+ }
106
+ }
107
+
108
+ ```
109
+
110
+
111
+
63
112
  中間テーブルはこのようにデータが入っています。
64
113
  |id|site_id|store_id|
65
114
  |:--|:--:|--:|

1

追記

2020/10/13 06:33

投稿

pup-ganbaru
pup-ganbaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -26,7 +26,17 @@
26
26
 
27
27
  ```ここに言語を入力
28
28
  StoreController
29
+ <?php
29
30
 
31
+ namespace App\Http\Controllers;
32
+
33
+ use Illuminate\Http\Request;
34
+ use \App\Models\Store;
35
+ use \App\Models\Site;
36
+
37
+ class StoreController extends Controller
38
+ {
39
+
30
40
  public function index()
31
41
  {
32
42
  $stores = Store::all();