質問編集履歴

1

Modelを追記しました。

2020/12/04 06:56

投稿

mikeko0901
mikeko0901

スコア227

test CHANGED
File without changes
test CHANGED
@@ -180,6 +180,232 @@
180
180
 
181
181
  ```
182
182
 
183
+
184
+
185
+ ■Model (User モデル)
186
+
187
+ ```
188
+
189
+ <?php
190
+
191
+
192
+
193
+ namespace App\Models;
194
+
195
+
196
+
197
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
198
+
199
+ use Illuminate\Database\Eloquent\Factories\HasFactory;
200
+
201
+ use Illuminate\Foundation\Auth\User as Authenticatable;
202
+
203
+ use Illuminate\Notifications\Notifiable;
204
+
205
+ use Laravel\Fortify\TwoFactorAuthenticatable;
206
+
207
+ use Laravel\Jetstream\HasProfilePhoto;
208
+
209
+ use Laravel\Sanctum\HasApiTokens;
210
+
211
+
212
+
213
+ use Illuminate\Database\Eloquent\Model; //追加してみた
214
+
215
+
216
+
217
+ class User extends Authenticatable
218
+
219
+ {
220
+
221
+ use HasApiTokens;
222
+
223
+ use HasFactory;
224
+
225
+ use HasProfilePhoto;
226
+
227
+ use Notifiable;
228
+
229
+ use TwoFactorAuthenticatable;
230
+
231
+
232
+
233
+ /**
234
+
235
+ * The attributes that are mass assignable.
236
+
237
+ *
238
+
239
+ * @var array
240
+
241
+ */
242
+
243
+ protected $fillable = [
244
+
245
+ 'name',
246
+
247
+ 'email',
248
+
249
+ 'password',
250
+
251
+ ];
252
+
253
+
254
+
255
+ /**
256
+
257
+ * The attributes that should be hidden for arrays.
258
+
259
+ *
260
+
261
+ * @var array
262
+
263
+ */
264
+
265
+ protected $hidden = [
266
+
267
+ 'password',
268
+
269
+ 'remember_token',
270
+
271
+ 'two_factor_recovery_codes',
272
+
273
+ 'two_factor_secret',
274
+
275
+ ];
276
+
277
+
278
+
279
+ public function followings() { //自分が誰をフォローしているか
280
+
281
+ //return $this->belongsToMany(User::class, 'user_follow', 'user_id', 'follow_id')->withTimestamps(); techの書き方
282
+
283
+ return $this->belongsToMany('App\Models\User', 'user_follow', 'user_id', 'follow_id')->withTimestamps();
284
+
285
+ }
286
+
287
+
288
+
289
+ public function followers() {
290
+
291
+ //return $this->belongsToMany(User::class, 'user_follow', 'follow_id', 'user_id')->withTimestamps(); techの書き方
292
+
293
+ return $this->belongsToMany('App\Models\User', 'user_follow', 'follow_id', 'user_id')->withTimestamps();
294
+
295
+ }
296
+
297
+
298
+
299
+ public function follow($userId) { //フォロー用の関数を定義
300
+
301
+ //すでにフォローしているかの確認
302
+
303
+ $exist = $this->is_following($userId);
304
+
305
+ //フォロー先が自分か
306
+
307
+ $its_me = $this->id == $userId;
308
+
309
+
310
+
311
+ if($exist || $its_me) {
312
+
313
+ return false;
314
+
315
+ }
316
+
317
+ else {
318
+
319
+ $this->followings()->attach($userId); //attach()で中間テーブルに登録する。削除はdetach
320
+
321
+ return true;
322
+
323
+ }
324
+
325
+ }
326
+
327
+
328
+
329
+ public function unfollow($userId) {
330
+
331
+ $exist = $this->is_following($userId);
332
+
333
+ $its_me = $this->id == $userId;
334
+
335
+
336
+
337
+ if($exist && !$its_me) {
338
+
339
+ $this->followings()->detach($userId);
340
+
341
+ return true;
342
+
343
+ }
344
+
345
+ else {
346
+
347
+ return false;
348
+
349
+ }
350
+
351
+ }
352
+
353
+
354
+
355
+ public function is_following($userId) {
356
+
357
+ return $this->followings()->where('follow_id', $userId)->exists(); //exists()でbooleanが戻る
358
+
359
+ }
360
+
361
+
362
+
363
+ /**
364
+
365
+ * The attributes that should be cast to native types.
366
+
367
+ *
368
+
369
+ * @var array
370
+
371
+ */
372
+
373
+ protected $casts = [
374
+
375
+ 'email_verified_at' => 'datetime',
376
+
377
+ ];
378
+
379
+
380
+
381
+ /**
382
+
383
+ * The accessors to append to the model's array form.
384
+
385
+ *
386
+
387
+ * @var array
388
+
389
+ */
390
+
391
+ protected $appends = [
392
+
393
+ 'profile_photo_url',
394
+
395
+ ];
396
+
397
+ }
398
+
399
+
400
+
401
+ ```
402
+
403
+
404
+
405
+
406
+
407
+
408
+
183
409
  フォルダ構成
184
410
 
185
411
  ![イメージ説明](71098c812cbcbb1a2d170d43d676e9c5.png)