Reputation: 1801
I am using Laravel 5.3 and I would like to make a query in requests file which I made to have some validation rules for a form where a user can edit his channel. In that file I would like to make a query which would look something like this:
$channelId = Auth::user()->channels()->where('id', $this->id)->get();
So that I can get the channel id and exclude it from the rules array, this is how a file looks like:
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',
];
}
I am not sure how to get the channel id
of that object that is being updated in the requests file?
Upvotes: 1
Views: 3245
Reputation: 11
I used model for that matter, in the request file we can access that object by $this and by model name using this we can access all property, So change just as below.
$channel = Auth::user()->channels()->where('id', $this->channel->id))->first();
But I am not doing like this, i am directly use $this->channel->id
in rule as below.
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',
];
Upvotes: 1
Reputation: 1801
I used a session for that matter, I have stored a key in edit function and then retrieved it in the request file in my query like this and now it works, and the user is not able to manipulate it in the form:
$channel = Auth::user()->channels()->where('id', session('channel_id'))->first();
Upvotes: 0
Reputation: 1054
When inside of a Request
object, you can access input as @Silwerclaw correctly said by calling $this->input("id")
when you have an input with name "id".
When outside of the object, you can use the facade: Request::input("id")
.
Upvotes: 1