2016-02-26 76 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

相关问题