我怎么能说Laravel,如果线程属于此用户,请允许他编辑或删除该线程


How do I can say Laravel that if the thread belongs to this user, allow him to edit or delete the thread?

好吧,我的标题几乎说明了我需要的一切。我正在为我建立一个小网站,只是为了学习laravel框架。我创建了一个登录/注册和一些函数来执行线程或删除/编辑线程。我目前的问题是,登录的用户是否与编写线程的用户相同并不重要。用户登录的事实,允许他编辑或删除我网页上的每一个线程。这当然不像是应该的...这就是为什么我喜欢得到这样的认识:如果登录的用户有一些线程,那么允许他删除或编辑自己的线程。如果用户不是编写话题的人,则根本不向他显示删除或编辑话题的选项。

现在这是我当前的HTML - 或者更好的是截取它:

   @if( Auth::check())
        <div class="panel-footer">
            {!! Former::horizontal_open()->method('DELETE')->action(action("Test''TestController@destroy", $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''TestController@index') }}">Go back</a></div>
    </a>

所以这只是检查用户是否已登录,如果没有,他看不到编辑/删除按钮。如果他登录了,他当然会看到它们。

现在我需要一个代码来说明他是否与编写线程的人相同,然后允许他编辑/删除它。

好吧,我

真的不知道我该怎么做,我还没有真正找到一些东西......

我有两个模型。一个用于所有线程,一个用于所有用户。

我在线程模型中做了一个"属于"的实现,并说它属于我的用户表中的 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''TestController@show');
Route::get('/show/{id}/edit', 'Test''TestController@edit')->name('edit');
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test''TestController@update']);
Route::delete('/show/{id}', 'Test''TestController@destroy')->name('destroy');

这就是我必须只显示线程或删除/编辑线程的所有路线

控制器:这是显示功能,它为我提供了带有按钮的视图:

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

你可以使用拉拉维尔的能力系统。

或者在控制器中编辑线程时,您可以执行以下操作:

$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,
    ]);
}
我认为做

你想要的最好的方法是使用 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
}

希望这有帮助!