2016-10-01 83 views
1

我使用Laravel 5.3,我想提出的请求文件的查询,我提出来有一个表单,用户可以编辑一些验证规则,他的渠道。在该文件中,我想作一个查询这将是这个样子:Laravel - 如何获取对象的参数的请求文件

$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

+0

你的意思是说,你不知道如何获得你使用表单发送的id(你使用的例子中是$ this-> id)? – Silwerclaw

+0

是的,这是正确的 – Marco

+0

你在里面Request对象,这意味着你可以使用它的内置功能,用于检索输入函数:$ this->输入(),$这个 - >输入(“ID”)。阅读更多在https://laravel.com/docs/5.3/requests#retrieving-input – Silwerclaw

回答

0

我曾经为此事会议上,我已存储在编辑功能键,然后在我这样的查询请求文件中检索它,现在它的工作原理,以及用户不能够操纵它的形式:

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

当您在Request对象中时,您可以通过调用$this->input("id")来正确访问输入为@Silwerclaw的输入,其名称为“id”。

当对象之外,你可以使用门面:Request::input("id")

+0

问题是我没有在表单中的ID输入,我想知道是否有办法避免这样做也许,因为我想避免用户有可能操纵id? – Marco

+0

是的,你可以做一个隐藏的输入是这样的:'”>' – MrPixelDream

+0

是的,我知道,希望我能避免 – Marco