質問編集履歴
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -136,4 +136,6 @@
|
|
136
136
|
|
137
137
|
> Property [books] does not exist on the Eloquent builder instance.
|
138
138
|
|
139
|
-
しかし、なぜか`first()`とすることで取得できた。
|
139
|
+
しかし、なぜか`first()`とすることで取得できた。
|
140
|
+
取得結果
|
141
|
+

|
4
修正した部分を追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -100,7 +100,7 @@
|
|
100
100
|
];
|
101
101
|
|
102
102
|
public function books(){
|
103
|
-
return $this->belongsToMany("App\Book");
|
103
|
+
return $this->belongsToMany("App\Book")->withTimestamps();
|
104
104
|
}
|
105
105
|
|
106
106
|
}
|
@@ -127,4 +127,13 @@
|
|
127
127
|
return $this->belongsToMany("App\User");
|
128
128
|
}
|
129
129
|
}
|
130
|
-
```
|
130
|
+
```
|
131
|
+
|
132
|
+
### 追記
|
133
|
+
|
134
|
+
`DB::table~latest();`だと単一のプロパティではないのか以下の様なエラーとなる。
|
135
|
+
|
136
|
+
|
137
|
+
> Property [books] does not exist on the Eloquent builder instance.
|
138
|
+
|
139
|
+
しかし、なぜか`first()`とすることで取得できた。
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,12 @@
|
|
4
4
|
リレーション定義に関しては[以前の質問](https://teratail.com/questions/291543)で解決することができたのですが、借りた履歴を引っ張ってきたのですが、借りた時間を取得したいにも関わらず、その本が投稿(追加された)時間の`created_at`が表示されてしまいます。
|
5
5
|
phpMyAdminで確認したところ、借りた時間はレコードに挿入されていました。
|
6
6
|
|
7
|
+
### 抽出したいカラムに関して
|
7
8
|
|
9
|
+
借りた時間というのは実際には借りた際にpostでレコードが挿入されますので、
|
10
|
+
create_bookテーブル(create_book_user_table.php)にある`timestamps`で`created_at`を表しています。
|
11
|
+
|
12
|
+
|
8
13
|
### 該当のソースコード
|
9
14
|
|
10
15
|
```PHP
|
2
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -34,14 +34,13 @@
|
|
34
34
|
$table->timestamps();
|
35
35
|
```
|
36
36
|
|
37
|
-
|
37
|
+
create_book_user_table.php
|
38
38
|
```PHP
|
39
39
|
$table->id();
|
40
|
-
$table->string('name');
|
41
|
-
$table->string('email')->unique();
|
42
|
-
$table->timestamp('email_verified_at')->nullable();
|
43
|
-
$table->
|
40
|
+
$table->unsignedBigInteger('user_id');
|
44
|
-
$table->
|
41
|
+
$table->unsignedBigInteger('book_id');
|
42
|
+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
43
|
+
$table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
|
45
44
|
$table->timestamps();
|
46
45
|
```
|
47
46
|
|
@@ -119,7 +118,7 @@
|
|
119
118
|
'book_name', 'book_price', 'book_detail',
|
120
119
|
];
|
121
120
|
|
122
|
-
public
|
121
|
+
public function users(){
|
123
122
|
return $this->belongsToMany("App\User");
|
124
123
|
}
|
125
124
|
}
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -21,4 +21,106 @@
|
|
21
21
|
|
22
22
|
Laravel 7.28.1
|
23
23
|
PHP 7.X
|
24
|
-
MySQL
|
24
|
+
MySQL
|
25
|
+
|
26
|
+
create_users_table.php
|
27
|
+
```PHP
|
28
|
+
$table->id();
|
29
|
+
$table->string('name');
|
30
|
+
$table->string('email')->unique();
|
31
|
+
$table->timestamp('email_verified_at')->nullable();
|
32
|
+
$table->string('password');
|
33
|
+
$table->rememberToken();
|
34
|
+
$table->timestamps();
|
35
|
+
```
|
36
|
+
|
37
|
+
create_user_table.php
|
38
|
+
```PHP
|
39
|
+
$table->id();
|
40
|
+
$table->string('name');
|
41
|
+
$table->string('email')->unique();
|
42
|
+
$table->timestamp('email_verified_at')->nullable();
|
43
|
+
$table->string('password');
|
44
|
+
$table->rememberToken();
|
45
|
+
$table->timestamps();
|
46
|
+
```
|
47
|
+
|
48
|
+
create_book_table.php
|
49
|
+
```
|
50
|
+
$table->id();
|
51
|
+
$table->char('book_name');
|
52
|
+
$table->bigInteger('book_price');
|
53
|
+
$table->text('book_detail');
|
54
|
+
$table->timestamps();
|
55
|
+
```
|
56
|
+
|
57
|
+
App\User.php
|
58
|
+
```PHP
|
59
|
+
<?php
|
60
|
+
|
61
|
+
namespace App;
|
62
|
+
|
63
|
+
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
64
|
+
use Illuminate\Foundation\Auth\User as Authenticatable;
|
65
|
+
use Illuminate\Notifications\Notifiable;
|
66
|
+
|
67
|
+
class User extends Authenticatable
|
68
|
+
{
|
69
|
+
use Notifiable;
|
70
|
+
|
71
|
+
/**
|
72
|
+
* The attributes that are mass assignable.
|
73
|
+
*
|
74
|
+
* @var array
|
75
|
+
*/
|
76
|
+
protected $fillable = [
|
77
|
+
'name', 'email', 'password',
|
78
|
+
];
|
79
|
+
|
80
|
+
/**
|
81
|
+
* The attributes that should be hidden for arrays.
|
82
|
+
*
|
83
|
+
* @var array
|
84
|
+
*/
|
85
|
+
protected $hidden = [
|
86
|
+
'password', 'remember_token',
|
87
|
+
];
|
88
|
+
|
89
|
+
/**
|
90
|
+
* The attributes that should be cast to native types.
|
91
|
+
*
|
92
|
+
* @var array
|
93
|
+
*/
|
94
|
+
protected $casts = [
|
95
|
+
'email_verified_at' => 'datetime',
|
96
|
+
];
|
97
|
+
|
98
|
+
public function books(){
|
99
|
+
return $this->belongsToMany("App\Book");
|
100
|
+
}
|
101
|
+
|
102
|
+
}
|
103
|
+
```
|
104
|
+
|
105
|
+
App\Book.php
|
106
|
+
|
107
|
+
```PHP
|
108
|
+
<?php
|
109
|
+
|
110
|
+
namespace App;
|
111
|
+
|
112
|
+
use Illuminate\Database\Eloquent\Model;
|
113
|
+
|
114
|
+
class Book extends Model
|
115
|
+
{
|
116
|
+
protected $table = "books";
|
117
|
+
|
118
|
+
protected $fillable = [
|
119
|
+
'book_name', 'book_price', 'book_detail',
|
120
|
+
];
|
121
|
+
|
122
|
+
public funciton users(){
|
123
|
+
return $this->belongsToMany("App\User");
|
124
|
+
}
|
125
|
+
}
|
126
|
+
```
|