質問編集履歴

3

deleted_user_id

2020/03/10 06:34

投稿

m1533854209
m1533854209

スコア8

test CHANGED
File without changes
test CHANGED
@@ -148,7 +148,7 @@
148
148
 
149
149
  {
150
150
 
151
- $model->lastmodified_id = 1;
151
+ $model-> deleted_user_id = 1;
152
152
 
153
153
  }
154
154
 
@@ -158,7 +158,7 @@
158
158
 
159
159
  {
160
160
 
161
- $model->lastmodified_id = 1;
161
+ $model-> deleted_user_id = 1;
162
162
 
163
163
  }
164
164
 
@@ -168,7 +168,7 @@
168
168
 
169
169
  {
170
170
 
171
- $model->lastmodified_id = 1;
171
+ $model-> deleted_user_id = 1;
172
172
 
173
173
  }
174
174
 
@@ -178,7 +178,7 @@
178
178
 
179
179
  {
180
180
 
181
- $model->lastmodified_id = 1;
181
+ $model-> deleted_user_id = 1;
182
182
 
183
183
  }
184
184
 
@@ -188,7 +188,7 @@
188
188
 
189
189
  {
190
190
 
191
- $model->lastmodified_id = 1;
191
+ $model-> deleted_user_id = 1;
192
192
 
193
193
  }
194
194
 
@@ -198,7 +198,7 @@
198
198
 
199
199
  {
200
200
 
201
- $model->lastmodified_id = 1;
201
+ $model-> deleted_user_id = 1;
202
202
 
203
203
  }
204
204
 
@@ -350,4 +350,4 @@
350
350
 
351
351
 
352
352
 
353
- ### どのタイミングで $model->lastmodified_id = 1;が実行されるのでしょうか?
353
+ ### どのタイミングで $model->deleted_user_id = 1;が実行されるのでしょうか?

2

Model

2020/03/10 06:34

投稿

m1533854209
m1533854209

スコア8

test CHANGED
File without changes
test CHANGED
@@ -115,3 +115,239 @@
115
115
 
116
116
 
117
117
  ```
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ ### Observer
128
+
129
+
130
+
131
+ ```Observers
132
+
133
+ <?php
134
+
135
+ namespace App\Observers;
136
+
137
+
138
+
139
+ use Illuminate\Database\Eloquent\Model;
140
+
141
+
142
+
143
+ class AuthorObserver
144
+
145
+ {
146
+
147
+ public function creating(Model $model)
148
+
149
+ {
150
+
151
+ $model->lastmodified_id = 1;
152
+
153
+ }
154
+
155
+
156
+
157
+ public function updating(Model $model)
158
+
159
+ {
160
+
161
+ $model->lastmodified_id = 1;
162
+
163
+ }
164
+
165
+
166
+
167
+ public function saving(Model $model)
168
+
169
+ {
170
+
171
+ $model->lastmodified_id = 1;
172
+
173
+ }
174
+
175
+
176
+
177
+ public function deleting(Model $model)
178
+
179
+ {
180
+
181
+ $model->lastmodified_id = 1;
182
+
183
+ }
184
+
185
+
186
+
187
+ public function restoring(Model $model)
188
+
189
+ {
190
+
191
+ $model->lastmodified_id = 1;
192
+
193
+ }
194
+
195
+
196
+
197
+ public function destroy(Model $model)
198
+
199
+ {
200
+
201
+ $model->lastmodified_id = 1;
202
+
203
+ }
204
+
205
+ }
206
+
207
+
208
+
209
+ ```
210
+
211
+
212
+
213
+
214
+
215
+ ```Traits
216
+
217
+ <?php
218
+
219
+ namespace App\Traits;
220
+
221
+
222
+
223
+ use App\Observers\AuthorObserver;
224
+
225
+ use Illuminate\Database\Eloquent\Model;
226
+
227
+
228
+
229
+ trait AuthorObservable
230
+
231
+ {
232
+
233
+ public static function bootAuthorObservable()
234
+
235
+ {
236
+
237
+ self::observe(AuthorObserver::class);
238
+
239
+ }
240
+
241
+ }
242
+
243
+ ```
244
+
245
+
246
+
247
+ ```Model
248
+
249
+ <?php
250
+
251
+
252
+
253
+ namespace App\Models;
254
+
255
+
256
+
257
+ use App\Traits\AuthorObservable;
258
+
259
+ use Illuminate\Database\Eloquent\Model;
260
+
261
+ use Illuminate\Database\Eloquent\SoftDeletes;
262
+
263
+
264
+
265
+ class User extends Model
266
+
267
+ {
268
+
269
+ use AuthorObservable;
270
+
271
+ use SoftDeletes;
272
+
273
+ protected $table = 'users';
274
+
275
+ protected $dates = ['deleted_at'];
276
+
277
+
278
+
279
+ public function shop()
280
+
281
+ {
282
+
283
+ return $this->belongsTo('App\Models\Shop');
284
+
285
+ }
286
+
287
+
288
+
289
+ public function created_by()
290
+
291
+ {
292
+
293
+ return $this->belongsTo('App\Models\User', 'created_by');
294
+
295
+ }
296
+
297
+
298
+
299
+ public function updated_by()
300
+
301
+ {
302
+
303
+ return $this->belongsTo('App\Models\User', 'updated_by');
304
+
305
+ }
306
+
307
+
308
+
309
+
310
+
311
+ }
312
+
313
+
314
+
315
+ ```
316
+
317
+
318
+
319
+ ```Controllers
320
+
321
+ public function destroy($user_id)
322
+
323
+ {
324
+
325
+ DB::beginTransaction();
326
+
327
+ try {
328
+
329
+ User::where('id', $user_id)->delete();
330
+
331
+ DB::commit();
332
+
333
+ } catch (\PDOException $e) {
334
+
335
+ DB::rollBack();
336
+
337
+ return false;
338
+
339
+ }
340
+
341
+ return true;
342
+
343
+ }
344
+
345
+ ```
346
+
347
+
348
+
349
+
350
+
351
+
352
+
353
+ ### どのタイミングで $model->lastmodified_id = 1;が実行されるのでしょうか?

1

```

2020/03/10 06:33

投稿

m1533854209
m1533854209

スコア8

test CHANGED
File without changes
test CHANGED
@@ -80,7 +80,7 @@
80
80
 
81
81
 
82
82
 
83
- ```
83
+
84
84
 
85
85
 
86
86