質問編集履歴

5

追記

2020/09/19 14:02

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -275,3 +275,7 @@
275
275
 
276
276
 
277
277
  しかし、なぜか`first()`とすることで取得できた。
278
+
279
+ 取得結果
280
+
281
+ ![イメージ説明](f2242983d7285f8890161cbde51d903b.png)

4

修正した部分を追記

2020/09/19 14:01

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -202,7 +202,7 @@
202
202
 
203
203
  public function books(){
204
204
 
205
- return $this->belongsToMany("App\Book");
205
+ return $this->belongsToMany("App\Book")->withTimestamps();
206
206
 
207
207
  }
208
208
 
@@ -257,3 +257,21 @@
257
257
  }
258
258
 
259
259
  ```
260
+
261
+
262
+
263
+ ### 追記
264
+
265
+
266
+
267
+ `DB::table~latest();`だと単一のプロパティではないのか以下の様なエラーとなる。
268
+
269
+
270
+
271
+
272
+
273
+ > Property [books] does not exist on the Eloquent builder instance.
274
+
275
+
276
+
277
+ しかし、なぜか`first()`とすることで取得できた。

3

追記

2020/09/19 12:28

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,16 @@
10
10
 
11
11
 
12
12
 
13
+ ### 抽出したいカラムに関して
14
+
15
+
16
+
17
+ 借りた時間というのは実際には借りた際にpostでレコードが挿入されますので、
18
+
19
+ create_bookテーブル(create_book_user_table.php)にある`timestamps`で`created_at`を表しています。
20
+
21
+
22
+
13
23
 
14
24
 
15
25
  ### 該当のソースコード

2

修正

2020/09/19 11:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -70,21 +70,19 @@
70
70
 
71
71
 
72
72
 
73
- create_user_table.php
73
+ create_book_user_table.php
74
74
 
75
75
  ```PHP
76
76
 
77
77
  $table->id();
78
78
 
79
- $table->string('name');
80
-
81
- $table->string('email')->unique();
82
-
83
- $table->timestamp('email_verified_at')->nullable();
84
-
85
- $table->string('password');
79
+ $table->unsignedBigInteger('user_id');
86
-
80
+
87
- $table->rememberToken();
81
+ $table->unsignedBigInteger('book_id');
82
+
83
+ $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
84
+
85
+ $table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
88
86
 
89
87
  $table->timestamps();
90
88
 
@@ -240,7 +238,7 @@
240
238
 
241
239
 
242
240
 
243
- public funciton users(){
241
+ public function users(){
244
242
 
245
243
  return $this->belongsToMany("App\User");
246
244
 

1

追記

2020/09/19 10:49

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,207 @@
45
45
  PHP 7.X
46
46
 
47
47
  MySQL
48
+
49
+
50
+
51
+ create_users_table.php
52
+
53
+ ```PHP
54
+
55
+ $table->id();
56
+
57
+ $table->string('name');
58
+
59
+ $table->string('email')->unique();
60
+
61
+ $table->timestamp('email_verified_at')->nullable();
62
+
63
+ $table->string('password');
64
+
65
+ $table->rememberToken();
66
+
67
+ $table->timestamps();
68
+
69
+ ```
70
+
71
+
72
+
73
+ create_user_table.php
74
+
75
+ ```PHP
76
+
77
+ $table->id();
78
+
79
+ $table->string('name');
80
+
81
+ $table->string('email')->unique();
82
+
83
+ $table->timestamp('email_verified_at')->nullable();
84
+
85
+ $table->string('password');
86
+
87
+ $table->rememberToken();
88
+
89
+ $table->timestamps();
90
+
91
+ ```
92
+
93
+
94
+
95
+ create_book_table.php
96
+
97
+ ```
98
+
99
+ $table->id();
100
+
101
+ $table->char('book_name');
102
+
103
+ $table->bigInteger('book_price');
104
+
105
+ $table->text('book_detail');
106
+
107
+ $table->timestamps();
108
+
109
+ ```
110
+
111
+
112
+
113
+ App\User.php
114
+
115
+ ```PHP
116
+
117
+ <?php
118
+
119
+
120
+
121
+ namespace App;
122
+
123
+
124
+
125
+ use Illuminate\Contracts\Auth\MustVerifyEmail;
126
+
127
+ use Illuminate\Foundation\Auth\User as Authenticatable;
128
+
129
+ use Illuminate\Notifications\Notifiable;
130
+
131
+
132
+
133
+ class User extends Authenticatable
134
+
135
+ {
136
+
137
+ use Notifiable;
138
+
139
+
140
+
141
+ /**
142
+
143
+ * The attributes that are mass assignable.
144
+
145
+ *
146
+
147
+ * @var array
148
+
149
+ */
150
+
151
+ protected $fillable = [
152
+
153
+ 'name', 'email', 'password',
154
+
155
+ ];
156
+
157
+
158
+
159
+ /**
160
+
161
+ * The attributes that should be hidden for arrays.
162
+
163
+ *
164
+
165
+ * @var array
166
+
167
+ */
168
+
169
+ protected $hidden = [
170
+
171
+ 'password', 'remember_token',
172
+
173
+ ];
174
+
175
+
176
+
177
+ /**
178
+
179
+ * The attributes that should be cast to native types.
180
+
181
+ *
182
+
183
+ * @var array
184
+
185
+ */
186
+
187
+ protected $casts = [
188
+
189
+ 'email_verified_at' => 'datetime',
190
+
191
+ ];
192
+
193
+
194
+
195
+ public function books(){
196
+
197
+ return $this->belongsToMany("App\Book");
198
+
199
+ }
200
+
201
+
202
+
203
+ }
204
+
205
+ ```
206
+
207
+
208
+
209
+ App\Book.php
210
+
211
+
212
+
213
+ ```PHP
214
+
215
+ <?php
216
+
217
+
218
+
219
+ namespace App;
220
+
221
+
222
+
223
+ use Illuminate\Database\Eloquent\Model;
224
+
225
+
226
+
227
+ class Book extends Model
228
+
229
+ {
230
+
231
+ protected $table = "books";
232
+
233
+
234
+
235
+ protected $fillable = [
236
+
237
+ 'book_name', 'book_price', 'book_detail',
238
+
239
+ ];
240
+
241
+
242
+
243
+ public funciton users(){
244
+
245
+ return $this->belongsToMany("App\User");
246
+
247
+ }
248
+
249
+ }
250
+
251
+ ```