반응형
언플러그드에서 행이 소프트 삭제되었는지 확인하는 방법은 무엇입니까?
라라벨 5.1에서 웅변적인 모델 개체가 소프트 삭제되었는지 확인할 수 있는 좋은 방법이 있습니까?나는 데이터를 선택하는 것을 말하는 것이 아니라 일단 목표를 갖게 되면 예를 들어.Thing::withTrashed()->find($id)
지금까지 내가 볼 수 있는 유일한 방법은
if ($thing->deleted_at !== null) { ... }
API에서 예를 들어 허용할 수 있는 관련 메서드가 없습니다.
if ($thing->isDeleted()) { ... }
제가 잘못된 API를 보고 있다는 것을 방금 깨달았습니다.Model 클래스에는 이것이 없지만 내 모델이 사용하는 SoftDelete 특성에는trashed()방법.
글을 쓸 수 있게요
if ($thing->trashed()) { ... }
larvel6에서는 다음을 사용할 수 있습니다.
Soft delete(소프트 삭제)를 사용하고 있는지 확인하려면 다음과 같이 하십시오.
if( method_exists($thing, 'trashed') ) {
// do something
}
Roupler Model이 리소스에서 소프트 삭제를 사용하고 있는지 확인하려면(리소스를 사용하여 응답할 때):
if( method_exists($this->resource, 'trashed') ) {
// do something
}
마지막으로 모델이 폐기되었는지 확인합니다.
if ($thing->trashed()) {
// do something
}
희망, 이것이 도움이 될 것입니다!
테스트 환경에 대한 해답을 찾는 사람들을 위해, Larvel의 테스트 사례 내에서 다음과 같이 주장할 수 있습니다.
$this->assertSoftDeleted($user);
또는 방금 삭제된 경우(소프트 삭제 없음)
$this->assertDeleted($user);
이게 최상의 길이에요.
$model = 'App\\Models\\ModelName';
$uses_soft_delete = in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model));
if($usesSoftDeletes) {
// write code...
}
이것은 나에게 효과가 있었습니다.
$checkDomain = Domain::where('tenant_id', $subdomain)->withTrashed()->first();
if($checkDomain->trashed()){
return redirect()->route('domain.not.found');
}else{
return view('frontend.' . theme() . '.index');
}
언급URL : https://stackoverflow.com/questions/34003284/how-to-check-if-row-is-soft-deleted-in-eloquent
반응형
'source' 카테고리의 다른 글
| NSUserDefaults의 모든 값을 가져올 수 있는 방법이 있습니까? (0) | 2023.08.05 |
|---|---|
| PowerShell에서 %ProgramFiles(x86)%와 같은 괄호가 있는 환경 변수 이름? (0) | 2023.08.05 |
| malloc "double free" 오류의 원인을 찾는 방법은 무엇입니까? (0) | 2023.08.05 |
| docker-compose.yml의 환경 변수 재사용 (0) | 2023.08.05 |
| 노드 JS 버전 번호가 혼동 (0) | 2023.08.05 |