简体   繁体   中英

What is the meaning of return $request->post(); in laravel?

what is the meaning of return $request->post(); in laravel? Please help me

public function store(Request $request)
{
    return $request->post();
}

Thanks for helping.

Request post() method

For example consider you have following data in request

[
        'username'=>'John lobo',
        "dob"=>'10/10/1990',
        'address'=>[
            'city'=>'London',
            'country'=>'Uk'
        ]
    ]

1.If no params passed to post() method then it return all data from request.

return $request->post(); 

Output will be

{
"username": "John lobo",
"dob": "10/10/1990",
"address": {
"city": "London",
"country": "Uk"
}
}

2.if first param passed to post() method then it will return only that keys value.For example i have passed

return $request->post('username');

then output will be John lobo

3.if first param passed to post() method is invalid key or doesn't exist in request then it will not throw error instead it will return null

4.Suppose if you want to set default value if its null then you can pass second param

return $request->post('username',"John Lobo"); 

5.In request post method you cant access nested key directly for example

return $request->post('address.city');

it wont throw error instead it will return null

But if you consider $request->input() then it works exactly same as $request->post() but one difference is,It will return nested value directly instead of empty

return request()->input('address.city');

The output is London

If request contains file then both request input and post return location of the path like below instead of file object.In that case $request->all() is good way

 "file" => "I:\xampp\tmp\phpB84E.tmptest"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM