2016-02-26 18 views
1

好吧,我的標題幾乎說明了我需要的一切。我正在爲我建立一個小網站,只是爲了學習laravel框架。我創建了一個登錄/註冊和一些函數來執行線程或刪除/編輯線程。我目前的問題是,登錄的用戶與寫一個線程的用戶是否相同並不重要。只是用戶登錄的事實,允許他編輯或刪除我的網頁上的每一個線程。這當然不是它應該是...這就是爲什麼我喜歡得到的說法:如果用戶登錄,有一些線程,然後讓他刪除或編輯自己的線程。如果用戶不是編寫該線程的用戶,則不要讓他顯示刪除或編輯該線程的選項。我怎麼能說Laravel,如果線程屬於這個用戶,讓他編輯或刪除線程?

現在這是我目前的HTML - 或它的更好文檔片斷:

@if(Auth::check()) 
     <div class="panel-footer"> 
      {!! Former::horizontal_open()->method('DELETE')->action(action("Test\\[email protected]", $thread->id))->id('conf') !!} 
      {!! Former::danger_submit('Delete') !!} 
      {!! Former::close() !!} 

      <a href="{{ URL::route('edit', $thread->id) }}"> 
      <div class="btn btn-primary">Edit</div> 
      </a> 
     </div> 
    @endif 
    </div> 
    <a href="#"> 
     <div class="btn pull-right"><a href="{{ action('Test\\[email protected]') }}">Go back</a></div> 
    </a> 

因此,這只是檢查,如果用戶登錄,如果沒有,他沒有看到編輯/刪除按鈕。如果他登錄,他當然會看到他們。

現在我需要一個代碼來說明如果他是誰寫的線程相同,然後讓他編輯/刪除它。

嗯,我真的不知道我怎麼能做到這一點,我還沒有真正找到了這個..

我有兩個型號。一個用於所有線程,另一個用於所有用戶。

我在我的線程模型中做了'belongsTo'realation,並且說它屬於我的用戶表中的name屬性。

線程模型:

<?php 
namespace App\Models\Thread; 

use Illuminate\Database\Eloquent\Model; 

class Thread extends Model { 
    public $table = 'thread'; 
    public $fillable = [ 
     'thread', 
     'content', 
    ]; 

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

用戶模型:

<?php 
namespace App\Models\Thread; 

use Illuminate\Database\Eloquent\Model; 

class User extends Model { 
    public $table = 'users'; 
    public $fillable = [ 
     'name', 
    ]; 
} 

好..我堅持,我希望有人能幫助我與此有關。

感謝所有幫助和支持

其他代碼部分我可以幫助:

Route::get('/show/{id}', 'Test\\[email protected]'); 
Route::get('/show/{id}/edit', 'Test\\[email protected]')->name('edit'); 
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\[email protected]']); 
Route::delete('/show/{id}', 'Test\\[email protected]')->name('destroy'); 

這就是我必須只顯示線程,或要刪除的路由/編輯線程

控制器:這是顯示功能,讓我用按鈕查看:

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

看看這個https://laravel.com/docs/5.2/authorization#defining-abilities –

+0

好吧,我看到了這一點,並瞭解它的大部分,但我更像一個laravel初學者,所以不能想象一下現在我可以從哪裏開始.. – ItzMe488

回答

1

您可以使用Laravel的ability系統。

或控制器中編輯線程時,你可以做這樣的事情:

$thread = Thread::findOrFail($thread_id); 

if (!Auth::check() && 
    $thread->user()->first()->id != Auth::user()->id) { 
    abort(404); // Stop the user 
} else { 
    // Edit the threat 
} 

編輯您的編輯:

這是否工作適合您嗎?

public function show($id) 
{ 
    $thread = Thread::findOrFail($id); 

    if (!Auth::check() && 
     $thread->user()->first()->id != Auth::user()->id) { 
     abort(404); // Stop the user 
    } 

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

我會試試你的代碼:)我唯一不明白的地方是 - > first()部分..這是幹什麼用的? 我真的很想使用laravel的能力系統,但我並不真正瞭解它:/我看到它,當然明白這意味着什麼,但我無法弄清楚我在哪裏開始等... – ItzMe488

+0

@ ItzMe488你可以嘗試'$ thread-> user-> id',基本上是一樣的。 –

+0

好吧..試過但沒有工作..我想我做了一些錯誤的事情..我不知道我應該把代碼放在哪裏。我將用我的show功能更新我的問題。我的show功能顯示刪除/編輯按鈕所在的頁面。因此,如果你點擊一個線程(在我有的線程列表中),那麼你將直接進入show route - > show函數,並且該函數會使用按鈕等指向HTML ...我將更新我的問題與一些事情,我不知道如何把代碼正確。 - 順便說一句,我做了我的數據庫的thread_id屬性。 – ItzMe488

1

我覺得做你想要使用Request類什麼的最佳途徑。你只需要創建一個新的請求:

<?php 
namespace App\Http\Requests; 

/** Remember to include here the classes you are using, 
    * in your case it would be something like this: 
    */ 
use App\Models\Thread; 
use Auth; 

class EditThreadRequest extends Request { 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     /** 
     * You can get the thread id from your view 
     * using for example a hidden input 
     * {!! Form::hidden('id', $thread->id) !!} 
     */ 
     $thread = Thread::findOrFail($this->get('id')); 
     return $thread->user->id === Auth::user()->id; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
     // Your validation rules 
     ]; 
    } 
} 

上述功能的情況下,該線程屬於返回true,以該用戶,允許用戶繼續提出請求。

這之後,您只需要在你這樣的編輯功能剛創建的請求,它會自動檢查用戶是否可以編輯螺紋:

public function edit (EditThreadRequest $request) 
{ 
    // Your code 
} 

希望這有助於!

+0

我只是問我是否得到了這個權利。我將執行一個請求文件(命名爲EditTereadRequest,並在那裏放入你給我的代碼,之後我將編輯我的編輯函數(和刪除函數的權利):------------ - 公共函數編輯(EditThreadRequest $ request,$ id){...}(和刪除函數相同)----對嗎? – ItzMe488

+0

沒錯,每次創建/編輯時都應該創建一個新的請求一些東西:CreateThreadRequest,EditThreadRequest ...這樣你可以驗證你的輸入字段並授權/不授權該請求。你可以通過命令行創建一個新的請求,如下所示:php artisan make:request EditThreadRequest。 –

+0

我試過了,但它給我一個:Class App \ Http \ Requests \ Auth \ EditThreadRequest不存在 - 當然我做了一個:使用App \ Http \ Requests \ Auth \ EditThreadRequest; – ItzMe488

相關問題