質問編集履歴
3
deleted_user_id
title
CHANGED
File without changes
|
body
CHANGED
@@ -73,32 +73,32 @@
|
|
73
73
|
{
|
74
74
|
public function creating(Model $model)
|
75
75
|
{
|
76
|
-
$model->
|
76
|
+
$model-> deleted_user_id = 1;
|
77
77
|
}
|
78
78
|
|
79
79
|
public function updating(Model $model)
|
80
80
|
{
|
81
|
-
$model->
|
81
|
+
$model-> deleted_user_id = 1;
|
82
82
|
}
|
83
83
|
|
84
84
|
public function saving(Model $model)
|
85
85
|
{
|
86
|
-
$model->
|
86
|
+
$model-> deleted_user_id = 1;
|
87
87
|
}
|
88
88
|
|
89
89
|
public function deleting(Model $model)
|
90
90
|
{
|
91
|
-
$model->
|
91
|
+
$model-> deleted_user_id = 1;
|
92
92
|
}
|
93
93
|
|
94
94
|
public function restoring(Model $model)
|
95
95
|
{
|
96
|
-
$model->
|
96
|
+
$model-> deleted_user_id = 1;
|
97
97
|
}
|
98
98
|
|
99
99
|
public function destroy(Model $model)
|
100
100
|
{
|
101
|
-
$model->
|
101
|
+
$model-> deleted_user_id = 1;
|
102
102
|
}
|
103
103
|
}
|
104
104
|
|
@@ -174,4 +174,4 @@
|
|
174
174
|
|
175
175
|
|
176
176
|
|
177
|
-
### どのタイミングで $model->
|
177
|
+
### どのタイミングで $model->deleted_user_id = 1;が実行されるのでしょうか?
|
2
Model
title
CHANGED
File without changes
|
body
CHANGED
@@ -56,4 +56,122 @@
|
|
56
56
|
protected $dates = ['deleted_at'];
|
57
57
|
}
|
58
58
|
|
59
|
-
```
|
59
|
+
```
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
### Observer
|
65
|
+
|
66
|
+
```Observers
|
67
|
+
<?php
|
68
|
+
namespace App\Observers;
|
69
|
+
|
70
|
+
use Illuminate\Database\Eloquent\Model;
|
71
|
+
|
72
|
+
class AuthorObserver
|
73
|
+
{
|
74
|
+
public function creating(Model $model)
|
75
|
+
{
|
76
|
+
$model->lastmodified_id = 1;
|
77
|
+
}
|
78
|
+
|
79
|
+
public function updating(Model $model)
|
80
|
+
{
|
81
|
+
$model->lastmodified_id = 1;
|
82
|
+
}
|
83
|
+
|
84
|
+
public function saving(Model $model)
|
85
|
+
{
|
86
|
+
$model->lastmodified_id = 1;
|
87
|
+
}
|
88
|
+
|
89
|
+
public function deleting(Model $model)
|
90
|
+
{
|
91
|
+
$model->lastmodified_id = 1;
|
92
|
+
}
|
93
|
+
|
94
|
+
public function restoring(Model $model)
|
95
|
+
{
|
96
|
+
$model->lastmodified_id = 1;
|
97
|
+
}
|
98
|
+
|
99
|
+
public function destroy(Model $model)
|
100
|
+
{
|
101
|
+
$model->lastmodified_id = 1;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
```
|
106
|
+
|
107
|
+
|
108
|
+
```Traits
|
109
|
+
<?php
|
110
|
+
namespace App\Traits;
|
111
|
+
|
112
|
+
use App\Observers\AuthorObserver;
|
113
|
+
use Illuminate\Database\Eloquent\Model;
|
114
|
+
|
115
|
+
trait AuthorObservable
|
116
|
+
{
|
117
|
+
public static function bootAuthorObservable()
|
118
|
+
{
|
119
|
+
self::observe(AuthorObserver::class);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
```
|
123
|
+
|
124
|
+
```Model
|
125
|
+
<?php
|
126
|
+
|
127
|
+
namespace App\Models;
|
128
|
+
|
129
|
+
use App\Traits\AuthorObservable;
|
130
|
+
use Illuminate\Database\Eloquent\Model;
|
131
|
+
use Illuminate\Database\Eloquent\SoftDeletes;
|
132
|
+
|
133
|
+
class User extends Model
|
134
|
+
{
|
135
|
+
use AuthorObservable;
|
136
|
+
use SoftDeletes;
|
137
|
+
protected $table = 'users';
|
138
|
+
protected $dates = ['deleted_at'];
|
139
|
+
|
140
|
+
public function shop()
|
141
|
+
{
|
142
|
+
return $this->belongsTo('App\Models\Shop');
|
143
|
+
}
|
144
|
+
|
145
|
+
public function created_by()
|
146
|
+
{
|
147
|
+
return $this->belongsTo('App\Models\User', 'created_by');
|
148
|
+
}
|
149
|
+
|
150
|
+
public function updated_by()
|
151
|
+
{
|
152
|
+
return $this->belongsTo('App\Models\User', 'updated_by');
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
}
|
157
|
+
|
158
|
+
```
|
159
|
+
|
160
|
+
```Controllers
|
161
|
+
public function destroy($user_id)
|
162
|
+
{
|
163
|
+
DB::beginTransaction();
|
164
|
+
try {
|
165
|
+
User::where('id', $user_id)->delete();
|
166
|
+
DB::commit();
|
167
|
+
} catch (\PDOException $e) {
|
168
|
+
DB::rollBack();
|
169
|
+
return false;
|
170
|
+
}
|
171
|
+
return true;
|
172
|
+
}
|
173
|
+
```
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
### どのタイミングで $model->lastmodified_id = 1;が実行されるのでしょうか?
|
1
```
title
CHANGED
File without changes
|
body
CHANGED
@@ -39,8 +39,8 @@
|
|
39
39
|
|
40
40
|
上記のようにはかけると思いますが、もう少し綺麗になるのかと探しています。
|
41
41
|
|
42
|
-
```
|
43
42
|
|
43
|
+
|
44
44
|
```models
|
45
45
|
<?php
|
46
46
|
|