JavaScript对象不断转换为Laravel后端的数组

发布于 2025-01-31 06:12:38 字数 1488 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉城 2025-02-07 06:12:38

对象概念在HTTP协议中不存在,也不能以请求作为对象发送数据。您可以将输入作为JSON对象获取并解码,但这将是 stdclass 对象。因此,您可以将请求数组分配给用户模型并将其用作对象。

例子:
假设您创建了用户模型,并且要将数组分配为属性。

$user = new User($request->user);

// or 

$user = new User();
$user->fill($request->user);

// or

$user = new User();
$user->name = $request->user['name']
$user->age = $request->user['age']
...

注意:最好在分配给用户对象之前验证输入。您可以使用表单请求

Object concept doesn't exist in HTTP protocol and you can't send your data with requests as an object. you can get the input as json object and decode it, but that will be a StdClass object. So, you can assign the request array to the user model and use it as an object.

Example:
Suppose you created a User model and you want to assign the array as an attribute.

$user = new User($request->user);

// or 

$user = new User();
$user->fill($request->user);

// or

$user = new User();
$user->name = $request->user['name']
$user->age = $request->user['age']
...

Note: It's better to validate the inputs before assign to the user object. you can do it clearly with Form Requests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文