소셜 네트워킹 사이트 유형의 프로젝트를 만들려고했는데 사용자가 로그인하면 해당 게시물을 삭제하려고 시도했지만 그 사람은 자신의 게시물 만 삭제할 수 있습니다. 다른 사람의 게시물을 좋아하고 싫어합니다. 내가 그것을 삭제하려고 할 때 는하지만, -이 편집하기 전에,Laravel : 비 대상 객체의 속성을 얻으려고 시도합니다.
Trying to get property of non-object
그리고 그것은 오류가 표시됩니다. 코드를 사용하여 다른 사람의 소식을 삭제할 수있는 사람 :
public function getDeletePost($post_id){
$post = Post::where('user_id',$post_id)->first();
if (Auth::user() != $post->user) {
return redirect()->back();
}
if ($post != null) {
$post->delete();
return redirect()->route('dashboard')->with(['message'=> 'Successfully deleted!!']);
}
return redirect()->route('dashboard')->with(['message'=> 'Wrong ID!!']);
}
"잘못된 ID!" 매번 게시물을 삭제하지 마십시오. 사용자 테이블의
열이 : 아이디, 이메일, 비밀번호 게시물 테이블의 컬럼 : 아이디, created_at, updated_at, 몸은 사용자 테이블의 ID로, 게시물 테이블 링크의 USER_ID를
을 USER_ID합니다.추가 된 DD (인증 : 사용자(), $ 게시물) :
User {#183 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
+exists: true
+wasRecentlyCreated: false
}
null
여기에 어떤 문제가 있습니까? 삭제 버튼을 포함하는 코드의
부 : dashboard.blade.php에서 :
<div class="interaction">
<a href="#">Like</a> |
<a href="#">Dislike</a>
@if(Auth::user() == $post->user)
|
<a href="#">Edit</a> |
<a href="{{route('post.delete',['post_id=>$post->id'])}}">Delete</a>
@endif
</div>
최초의'if' 전에 넣어주세요 출력이'DD (인증 : 사용자(), $ 게시물)이 무엇인지 우리에게 보여; ' –
@Alexey Mezenin - 그랬습니까, 지금 확인하십시오. –
감사합니다. 나는 asnwer를 게시했다. –