如何在请求文件中获取对象的参数


Laravel - how to get a parameter of an object in a request file

我使用的是Laravel 5.3,我想在请求文件中进行查询,我为用户可以编辑他的频道的表单设置了一些验证规则。在该文件中,我想创建一个查询,看起来像这样:

$channelId = Auth::user()->channels()->where('id', $this->id)->get();
因此,我可以获得通道id并将其从规则数组中排除,这就是文件的样子:
public function rules()
    {
        $channelId = Auth::user()->channels()->where('id', $this->id)->get();
        return [
            'name' => 'required|max:255|unique:channels,name,' . $channelId,
            'slug' => 'required|max:255|alpha_num|unique:channels,slug,' . $channelId,
            'description' => 'max:1000',
        ];
    }

我不确定如何获得在请求文件中正在更新的对象的channel id ?

当在Request对象内部时,当您有名称为"id"的输入时,您可以通过调用$this->input("id")正确地访问输入@ silverclaw。

在对象外部时,可以使用facade: Request::input("id") .

我使用了model,在请求文件中,我们可以通过$this和模型名访问该对象,使用this我们可以访问所有属性,因此更改如下:

$channel = Auth::user()->channels()->where('id', $this->channel->id))->first();

但我不这样做,我直接使用$this->channel->id规则如下。

return [
        'name' => 'required|max:255|unique:channels,name,' . $this->channel->id,
        'slug' => 'required|max:255|alpha_num|unique:channels,slug,' . $this->channel->id,
        'description' => 'max:1000',
    ];

我使用了一个会话,我在编辑函数中存储了一个键,然后在我的查询中的请求文件中检索它,现在它工作了,用户无法以以下形式操作它:

$channel = Auth::user()->channels()->where('id', session('channel_id'))->first();