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

質問編集履歴

3

記述ミス

2021/07/21 09:54

投稿

n18marron
n18marron

スコア3

title CHANGED
File without changes
body CHANGED
@@ -453,10 +453,10 @@
453
453
 
454
454
  @section('content')
455
455
  @foreach($datas as $data)
456
- {{ $data->id }}
456
+ id{{ $data->id }}
457
- {{ $data->books->title }}
457
+ 本の名前{{ $data->books->title }}
458
- {{ $data->users->name }}
458
+ 借りての名前{{ $data->users->name }}
459
- {{ $data->return_date }}
459
+ 貸し出し日{{ $data->lent_date }}
460
460
  <br>
461
461
  @endforeach
462
462
  @endsection

2

whereNullに変更してエラー文を追加しました。

2021/07/21 09:54

投稿

n18marron
n18marron

スコア3

title CHANGED
File without changes
body CHANGED
@@ -7,9 +7,67 @@
7
7
  ### 発生している問題・エラーメッセージ
8
8
 
9
9
  ```
10
- エラーメッセージはありません
10
+ Trying to get property 'title' of non-object
11
11
  ```
12
-
12
+ dd(&datas)をしてみたところ
13
+ ```
14
+ Illuminate\Database\Eloquent\Collection {#310 ▼
15
+ #items: array:1 [▼
16
+ 0 => App\Models\Lending {#312 ▼
17
+ #table: "lendings"
18
+ #fillable: array:5 [▼
19
+ 0 => "user_id"
20
+ 1 => "book_id"
21
+ 2 => "lent_date"
22
+ 3 => "return_date"
23
+ 4 => "status"
24
+ ]
25
+ +timestamps: false
26
+ #connection: "sqlite"
27
+ #primaryKey: "id"
28
+ #keyType: "int"
29
+ +incrementing: true
30
+ #with: []
31
+ #withCount: []
32
+ +preventsLazyLoading: false
33
+ #perPage: 15
34
+ +exists: true
35
+ +wasRecentlyCreated: false
36
+ #attributes: array:6 [▼
37
+ "id" => "2"
38
+ "user_id" => "2"
39
+ "book_id" => "3"
40
+ "lent_date" => "2021-6-30"
41
+ "return_date" => null
42
+ "status" => "0"
43
+ ]
44
+ #original: array:6 [▼
45
+ "id" => "2"
46
+ "user_id" => "2"
47
+ "book_id" => "3"
48
+ "lent_date" => "2021-6-30"
49
+ "return_date" => null
50
+ "status" => "0"
51
+ ]
52
+ #changes: []
53
+ #casts: []
54
+ #classCastCache: []
55
+ #dates: []
56
+ #dateFormat: null
57
+ #appends: []
58
+ #dispatchesEvents: []
59
+ #observables: []
60
+ #relations: []
61
+ #touches: []
62
+ #hidden: []
63
+ #visible: []
64
+ #guarded: array:1 [▼
65
+ 0 => "*"
66
+ ]
67
+ }
68
+ ]
69
+ }
70
+ ```
13
71
  ### 該当のソースコード
14
72
  ###マイグレーションファイル
15
73
  ```
@@ -384,7 +442,7 @@
384
442
  // 貸出中の本を表示
385
443
  public function index()
386
444
  {
387
- $datas = Lending::where('return_date', null)->get();
445
+ $datas = Lending::whereNull('return_date')->get();
388
446
  return view('index', ['datas' => $datas]);
389
447
  }
390
448
  }

1

seederファイルの追加

2021/07/21 09:46

投稿

n18marron
n18marron

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,7 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
3
  表題の通りです。
4
- ビューで貸し出しテーブルにリレーションしたusersテーブルのnameとbooksテーブルのtitleを表示させたいです。
4
+ ビューで貸し出しテーブルにリレーションしたusersテーブルのnameとbooksテーブルのtitleを表示させたいです。そのうえで貸し出しテーブルのreturn_dateがnullの値を抽出したいです。
5
5
  ですがリレーション先へのアクセスができなくて詰まっています。
6
6
 
7
7
  ### 発生している問題・エラーメッセージ
@@ -272,6 +272,102 @@
272
272
  }
273
273
  }
274
274
  ```
275
+ ### シーダー
276
+ ```
277
+ // database/seeder/UserSeeder
278
+ <?php
279
+
280
+ namespace Database\Seeders;
281
+
282
+ use Illuminate\Database\Seeder;
283
+ use Illuminate\Support\Facades\DB;
284
+ use Illuminate\Support\Facades\Hash;
285
+
286
+ class UserSeeder extends Seeder
287
+ {
288
+ /**
289
+ * Run the database seeds.
290
+ *
291
+ * @return void
292
+ */
293
+ public function run()
294
+ {
295
+ for ($i = 1; $i <= 10; $i++) {
296
+ DB::table('users')->insert([
297
+ 'id' => $i,
298
+ 'name' => 'user' . $i,
299
+ 'email' => 'email' . $i . '@gmail.com',
300
+ 'password' => Hash::make('password'),
301
+ ]);
302
+ }
303
+ }
304
+ }
305
+
306
+ ```
307
+ ```
308
+ //database/seeder/BookSeeder
309
+ <?php
310
+
311
+ namespace Database\Seeders;
312
+
313
+ use Illuminate\Database\Seeder;
314
+ use Illuminate\Support\Facades\DB;
315
+
316
+ class BookSeeder extends Seeder
317
+ {
318
+ /**
319
+ * Run the database seeds.
320
+ *
321
+ * @return void
322
+ */
323
+ public function run()
324
+ {
325
+ for ($i = 1; $i <= 10; $i++) {
326
+ DB::table('books')->insert([
327
+ 'title' => 'title' . $i,
328
+ ]);
329
+ }
330
+ }
331
+ }
332
+
333
+ ```
334
+ ```
335
+ // database/seeder/LendingSeeder
336
+ <?php
337
+
338
+ namespace Database\Seeders;
339
+
340
+ use Illuminate\Support\Facades\DB;
341
+ use Illuminate\Database\Seeder;
342
+
343
+ class LendingSeeder extends Seeder
344
+ {
345
+ /**
346
+ * Run the database seeds.
347
+ *
348
+ * @return void
349
+ */
350
+ public function run()
351
+ {
352
+ DB::table('lendings')->insert([
353
+ 'user_id' => '1',
354
+ 'book_id' => '2',
355
+ 'lent_date' => '2021-6-30',
356
+ 'return_date' => '2021-7-4',
357
+ 'status' => '0',
358
+ ]);
359
+
360
+ DB::table('lendings')->insert([
361
+ 'user_id' => '2',
362
+ 'book_id' => '3',
363
+ 'lent_date' => '2021-6-30',
364
+ 'return_date' => null,
365
+ 'status' => '0',
366
+ ]);
367
+ }
368
+ }
369
+
370
+ ```
275
371
  ### コントローラ
276
372
  ```
277
373
  // App/Http/Controller/LendingController