laravel 在对象数组中添加属性和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34638762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:59:22  来源:igfitidea点击:

Add property and value in a object array

phplaravel

提问by Marco Santos

im quite new in php OOP, and im using laravel has my framework of choice. Im creating a object user and create the record, the only thing i have to do is to check if one of my input fields(not required) was set, if it was set than i have to add in in the user object, but how can i add this property (mobilephone) and value in a exiting object, am i doing it right?.

我在 php OOP 中很新,我使用 laravel 有我选择的框架。我创建了一个对象用户并创建了记录,我唯一要做的就是检查是否设置了我的一个输入字段(不是必需的),如果设置了它,我必须在用户对象中添加,但是如何我可以在现有的对象中添加这个属性(手机)和值,我做对了吗?。

code:

代码:

$user = User::create([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if (isset($userDat['mobilephone'])) {
    $user->mobilephone = $userDat['mobilephone'];
}

回答by Ben Swinburne

There're a few ways you can do it.

有几种方法可以做到。

Prepare the array in advance

提前准备好阵列

$data = [
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
];

if ($phone = array_get($userDat, 'mobilephone')) {
    $data['mobilephone'] = $phone;
}

User::create($data);

Or create the object first then save it

或者先创建对象然后保存

$user = new User([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if ($phone = array_get($userDat, 'mobilephone')) {
    $user->mobilephone = $phone;
}

$user->save();

As you are currently, but with an extra query (Ill advised)

就像你现在一样,但有一个额外的查询(不建议)

$user = User::create([
    'email'              => $userDat['email'],
    'name'               => $userDat['name'],
    'surname'            => $userDat['surname'],
]);

if ($phone = array_get($userDat, 'mobilephone')) {
    $user->mobilephone = $phone;

    $user->save();
}

回答by helmbert

how can i add this property (mobilephone) and value in a exiting object, am i doing it right?.

如何在现有对象中添加此属性(手机)和值,我做对了吗?

This strongly depends on which framework you are using. In general, you canadd arbitrary properties to any object in PHP (which does not mean you always should).

这在很大程度上取决于您使用的框架。通常,您可以向 PHP 中的任何对象添加任意属性(这并不意味着您总是应该这样做)。

In your case, you are using the Eloquentframework that's shipped with Laravel, which actually dependson dynamically assigned object attributes and are using it absolutely correctly. This is documented in-depth in the official documentation. Note that you'll probably have to call $user->save()at some point to actually save your user object (thanks to Ben for the hint).

在你的情况,你使用的是雄辩的框架,真实随Laravel,这实际上取决于对动态分配的对象属性,并绝对正确使用它。这在官方文档中有详细记录。请注意,您可能必须$user->save()在某个时候调用才能实际保存您的用户对象(感谢 Ben 的提示)。

Note that alternatively, you could assign allproperties of your Userinstance that way:

请注意,或者,您可以通过这种方式分配实例的所有属性User

$user = new User();
$user->email   = $userDat['email'];
$user->name    = $userDat['name'];
$user->surname = $userDat['surname'];

if (isset($userDat['mobilephone'])) {
    $user->mobilephone = $userDat['mobilephone'];
}

$user->save();

回答by Alaa M. Jaddou

If you absolutely have to add the property to the object, I believe you could cast it as an array, add your property (as a new array key), then cast it back as an object. The only time you run into stdClass objects (I believe) is when you cast an array as an object or when you create a new stdClass object from scratch (and of course when you json_decode() something - silly me for forgetting!).

如果您绝对必须将属性添加到对象中,我相信您可以将其转换为数组,添加您的属性(作为新的数组键),然后将其转换为对象。您遇到 stdClass 对象的唯一时间(我相信)是当您将数组转换为对象或从头开始创建新的 stdClass 对象时(当然,当您使用 json_decode() 某些东西时-我忘记了!)。

Instead of:

代替:

$foo = new StdClass();
$foo->bar = '1234';

You'd do:

你会这样做:

$foo = array('bar' => '1234');
$foo = (object)$foo;

Or if you already had an existing stdClass object:

或者,如果您已经有一个现有的 stdClass 对象:

$foo = (array)$foo;
$foo['bar'] = '1234';
$foo = (object)$foo;

Also as a 1 liner:

也作为 1 班轮:

$foo = (object) array_merge( (array)$foo, array( 'bar' => '1234' ) );


or maybe you could do it this way

或者你可以这样做

$user->{'mobilephone'} = $userDat['mobilephone'];