質問編集履歴

2

phoneモデルを追加致しました。

2016/09/16 05:59

投稿

AlwaysOreHungry
AlwaysOreHungry

スコア33

test CHANGED
File without changes
test CHANGED
@@ -169,3 +169,39 @@
169
169
  }
170
170
 
171
171
  ```
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ ```php
180
+
181
+ <?php
182
+
183
+
184
+
185
+ namespace App;
186
+
187
+
188
+
189
+ use Illuminate\Database\Eloquent\Model;
190
+
191
+
192
+
193
+ class Phone extends Model
194
+
195
+ {
196
+
197
+ protected $fillable = ['user_id','phone_num'];
198
+
199
+
200
+
201
+ protected $table = 'phones';
202
+
203
+
204
+
205
+ }
206
+
207
+ ```

1

追記のようコード修正しましたが、上手くいきませんでした。

2016/09/16 05:59

投稿

AlwaysOreHungry
AlwaysOreHungry

スコア33

test CHANGED
File without changes
test CHANGED
@@ -65,3 +65,107 @@
65
65
  }
66
66
 
67
67
  ```
68
+
69
+
70
+
71
+ ```php
72
+
73
+ <?php
74
+
75
+
76
+
77
+ namespace App;
78
+
79
+
80
+
81
+ use Illuminate\Foundation\Auth\User as Authenticatable;
82
+
83
+ use App\Phone;
84
+
85
+
86
+
87
+ class User extends Authenticatable
88
+
89
+ {
90
+
91
+ /**
92
+
93
+ * The attributes that are mass assignable.
94
+
95
+ *
96
+
97
+ * @var array
98
+
99
+ */
100
+
101
+ protected $fillable = [
102
+
103
+ 'name', 'email', 'password',
104
+
105
+ ];
106
+
107
+
108
+
109
+ /**
110
+
111
+ * The attributes that should be hidden for arrays.
112
+
113
+ *
114
+
115
+ * @var array
116
+
117
+ */
118
+
119
+ protected $hidden = [
120
+
121
+ 'password', 'remember_token',
122
+
123
+ ];
124
+
125
+
126
+
127
+
128
+
129
+ public function phone()
130
+
131
+ {
132
+
133
+ return $this->hasOne(Phone::class, 'id', 'user_id');
134
+
135
+ }
136
+
137
+ }
138
+
139
+
140
+
141
+ ```
142
+
143
+
144
+
145
+ ```php
146
+
147
+ public function mypage( $name, $id )
148
+
149
+ {
150
+
151
+ //Controller記載部分抜粋(※後にWhere('id',$id)で個人の携帯を紐づける予定です)
152
+
153
+ $users = User::all();
154
+
155
+
156
+
157
+ foreach($users as $user) {
158
+
159
+ $phoneNumList[] = $user->phone->phone_num; // Userモデルで定義したphoneメソッド
160
+
161
+ }
162
+
163
+ echo $phoneNumList;
164
+
165
+
166
+
167
+
168
+
169
+ }
170
+
171
+ ```