2016-03-07 20 views
1

我正在做一個Laravel框架的博客,我已經有一個登錄/註冊和一個線程部分。在我的博客中,如果你已經登錄,你可以編輯一個線程。現在我遇到了如果我登錄的問題,我可以編輯和刪除每個線程。不管它是我的線索還是來自其他用戶。那麼現在我需要說一些我可以編輯/刪除自己的線程的laravel代碼。我怎麼說Laravel,如果線程屬於這個用戶,讓他

我發現這一點:https://laravel.com/docs/5.2/authorization#defining-abilities

但我真的不明白,我怎麼實現這個我的代碼。我需要一個在我的數據庫中的任何參考?像這個用戶屬於這個線程?

嗯,我在laravel一種新的。我希望有人能幫助我

PS:我很抱歉我的英文不好,我來自德國。

編輯/更新/刪除功能:

public function edit($id) 
    { 
     $thread = Thread::query()->findOrFail($id); 
     return view('test.edit', [ 
      'thread' => $thread 
     ]); 
    } 

    public function update($id, StoreRequest $request) 
    { 
     $thread = Thread::query()->findOrFail($id); 
     $thread->fill($request->all()); 
     $thread->save(); 
     return redirect(action('Test\\[email protected]', [$thread->id])); 
    } 

    public function destroy($id) 
    { 
     $thread = Thread::query()->findOrFail($id); 
     $thread->delete(); 
     return redirect(action("Test\\[email protected]")); 
    } 

我的線程模型:

public function user() { 
    return $this->belongsTo(User::class, "name"); 
} 

我如何添加一個新的線程:

如果我按 「添加線程」 我在我的控制器中定向到我的添加功能:

添加功能:

public function add() 
    { 
     return view('test.add', [ 
      'entries' => Thread::query()->get() 
     ]); 
    } 
在我add.blade

我有我的表現公式,這公式推指引我到我的「存儲功能」在我的控制器:

存儲功能:

public function store(StoreRequest $request) 
    { 
     Thread::create($request->all()); 
     return redirect(action('Test\\[email protected]')); 
    } 

回答

1

您可以將USER_ID重視該線程隨時想要更新或刪除您檢查當前登錄用戶是否承擔該user_id,然後您做相應的。

添加USER_ID線程

然後在你的保存功能()做到這一點。

public function save(Request $request){ 
    $thread = new Thread; 
    $thread->user_id = Auth::user()->id; 
    // rest of fields goes here 
    $thread->save(); 
} 

然後在編輯,更新或刪除功能

public function edit($id) 
{ 
    $thread = Thread::query()->findOrFail($id); 

    // You can use laravel authorization/policies to achieve this too 
    if($thread->user_id != Auth::user()->id){ 
     // Return to view with your custom error message telling 
     // the user he is not authorized to edit this thread 
    } 

    return view('test.edit', [ 
     'thread' => $thread 
    ]); 
} 

public function update($id, StoreRequest $request) 
{ 
    $thread = Thread::query()->findOrFail($id); 

    // You can use laravel authorization/policies to achieve this too 
    if($thread->user_id != Auth::user()->id){ 
     // Return to view with your custom error message telling 
     // the user he is not authorized to edit this thread 
    } 

    $thread->fill($request->all()); 
    $thread->save(); 
    return redirect(action('Test\\[email protected]', [$thread->id])); 
} 

public function destroy($id) 
{ 
    $thread = Thread::query()->findOrFail($id); 

    // You can use laravel authorization/policies to achieve this too 
    if($thread->user_id != Auth::user()->id){ 
     // Return to view with your custom error message telling 
     // the user he is not authorized to delete this thread 
    } 

    $thread->delete(); 
    return redirect(action("Test\\[email protected]")); 
} 
+0

MHH你能告訴我,我怎麼能在一個更具體的方式做到這一點? – ItzMe488

+0

我已經更新了我的答案 – oseintow

+0

我這樣做,它幾乎完美。我在登錄時創建了一個新線程。在我寫了一個新線程後,我嘗試更新線程並收到錯誤消息。我得到了錯誤消息,導致我的線程表中的user_id與我的用戶表中的id不一樣。 - 我不太瞭解這個保存功能。在你寫的save函數中://其餘的字段在這裏----你的意思是我的公式添加一個線程?因爲我的公式是在我看來。對不起,所有這些,我還是新來laravel:/ – ItzMe488

相關問題