How to get the key from a laravel form request?
Use $key => $value
notation in foreach
:
foreach ($request->except('_token') as $key => $part) {
// $key gives you the key. 2 and 7 in your case.
}
$data = $request->except('_token')
foreach($data as $id => $value){
echo "My id is ". $id . " And My value is ". $value;
}
If You want to access exactly one $request
key, You can use:
$request->key_name
to achieve this.
Example
I was sending an image
in postman like this:
And I got it in Laravel's controller like this:
public function store(SendMessageRequest $request)
{
$image = $request->image; // this is what You need :)
// ...
}
Use Collection
for a one-lined code instead of foreach()
.
$requestKeys = collect($request->all())->keys();