質問編集履歴

2

UserモデルとBlogモデルの追加

2021/03/02 02:20

投稿

tkm-mkzk
tkm-mkzk

スコア4

test CHANGED
File without changes
test CHANGED
@@ -222,6 +222,186 @@
222
222
 
223
223
 
224
224
 
225
+ ```php
226
+
227
+ <?php
228
+
229
+
230
+
231
+ //Models/User.php
232
+
233
+
234
+
235
+ namespace App\Models;
236
+
237
+
238
+
239
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
240
+
241
+ use Illuminate\Database\Eloquent\Factories\HasFactory;
242
+
243
+ use Illuminate\Foundation\Auth\User as Authenticatable;
244
+
245
+ use Illuminate\Notifications\Notifiable;
246
+
247
+
248
+
249
+ class User extends Authenticatable
250
+
251
+ {
252
+
253
+ use HasFactory, Notifiable;
254
+
255
+
256
+
257
+ /**
258
+
259
+ * The attributes that are mass assignable.
260
+
261
+ *
262
+
263
+ * @var array
264
+
265
+ */
266
+
267
+ protected $fillable = [
268
+
269
+ 'name',
270
+
271
+ 'email',
272
+
273
+ 'password',
274
+
275
+ ];
276
+
277
+
278
+
279
+ /**
280
+
281
+ * The attributes that should be hidden for arrays.
282
+
283
+ *
284
+
285
+ * @var array
286
+
287
+ */
288
+
289
+ protected $hidden = [
290
+
291
+ 'password',
292
+
293
+ 'remember_token',
294
+
295
+ ];
296
+
297
+
298
+
299
+ /**
300
+
301
+ * The attributes that should be cast to native types.
302
+
303
+ *
304
+
305
+ * @var array
306
+
307
+ */
308
+
309
+ protected $casts = [
310
+
311
+ 'email_verified_at' => 'datetime',
312
+
313
+ ];
314
+
315
+
316
+
317
+ public function blogs(){
318
+
319
+ return $this->hasMany('App\Models\Blog');
320
+
321
+ }
322
+
323
+
324
+
325
+ public function weights(){
326
+
327
+ return $this->hasMany('App\Models\Weight');
328
+
329
+ }
330
+
331
+
332
+
333
+ public function selectUserFindById($id)
334
+
335
+ {
336
+
337
+ $query = $this->select([
338
+
339
+ 'id',
340
+
341
+ 'name',
342
+
343
+ 'email'
344
+
345
+ ])->where([
346
+
347
+ 'id' => $id
348
+
349
+ ]);
350
+
351
+ // first()は1件のみ取得する関数
352
+
353
+ return $query->first();
354
+
355
+ }
356
+
357
+ }
358
+
359
+ ```
360
+
361
+
362
+
363
+ ```php
364
+
365
+ <?php
366
+
367
+
368
+
369
+ //Models/Blog.php
370
+
371
+
372
+
373
+ namespace App\Models;
374
+
375
+
376
+
377
+ use Illuminate\Database\Eloquent\Factories\HasFactory;
378
+
379
+ use Illuminate\Database\Eloquent\Model;
380
+
381
+
382
+
383
+ class Blog extends Model
384
+
385
+ {
386
+
387
+ use HasFactory;
388
+
389
+
390
+
391
+ public function user(){
392
+
393
+ return $this->belongsTo('App\Models\User');
394
+
395
+ }
396
+
397
+
398
+
399
+ }
400
+
401
+ ```
402
+
403
+
404
+
225
405
  ### 補足情報(FW/ツールのバージョンなど)
226
406
 
227
407
 

1

いらないコメントの削除

2021/03/02 02:20

投稿

tkm-mkzk
tkm-mkzk

スコア4

test CHANGED
File without changes
test CHANGED
@@ -42,8 +42,6 @@
42
42
 
43
43
  $query = DB::table('blogs');
44
44
 
45
- // ->join('blogs', 'blog.id', '=', 'blogs.user_id')
46
-
47
45
 
48
46
 
49
47
  // もしキーワードがあったら