简体   繁体   English

在Laravel请求对象中使用变量名

[英]Using variable name in Laravel request object

I need to be able to loop through a list of laravel request variables and do something with them. 我需要能够遍历laravel请求变量列表并对其进行处理。 I want to be able to use a variable when calling the request object so that I can run it in a loop instead of writing a line of code for every one. 我希望能够在调用请求对象时使用变量,以便可以循环运行它,而不是为每个代码编写一行代码。

For example, my text inputs may have names that look something like this 例如,我的文本输入中的名称可能看起来像这样

contact_main_name
contact_main_telephone
contact_main_email

contact_sub_name
contact_sub_telephone
contact_sub_email

contact_backup_name
contact_backup_telephone
contact_backup_email

In my request, I don't want to have to write 在我的请求中,我不想写

$request->contact_main_name
$request->contact_main_telephone

For each different type of contact I may have, I want to be able to loop through them like so 对于我可能拥有的每种不同类型的联系人,我希望能够像这样遍历它们

$contactTypes = [
    'main',
    'sub',
    'backup',
    'head'
];

    foreach($contactTypes as $type){
        //Start a new contact
        $contact = new Contact;
        $contact->type = $type;
        $contact->name = $request->${"contact_".$type."_name"};
        $contact->telephone = $request->${"contact_".$type."_telephone"};
        $contact->email = $request->${"contact_".$type."_email"};
        $contact->save();
    }

How would i use a variable name when calling a laravel $request so that I can just build an array of possible types and loop through them all? 调用laravel $request ,我将如何使用变量名,以便我可以构建可能类型的数组并遍历所有这些类型?

Note I know i can edit the input fields themselves to look something like name="contact[type][name]" and then loop through them, but I cant be changing the input names, I have to do it via php in the controller itself. 注意我知道我可以自己编辑输入字段,使其看起来像name="contact[type][name]" ,然后遍历它们,但是我不能更改输入名称,我必须通过控制器中的php来完成本身。

As answered in comments, to do this, change the method of calling the input and use the actual input() function itself. 正如评论中所回答的那样,为此,请更改调用输入的方法,并使用实际的input()函数本身。

$contactTypes = [
    'main',
    'sub',
    'backup',
    'head'
];

foreach($contactTypes as $type){
    //Start a new contact
    $contact = new Contact;
    $contact->type = $type;
    $contact->name = $request->input("contact_".$type."_name");
    $contact->telephone = $request->input("contact_".$type."_telephone");
    $contact->email = $request->input("contact_".$type."_email");
    $contact->save();
}

As an aside, you could also modify it slightly to use array indices matching the field names; 顺便说一句,您也可以对其进行一些修改,以使用与字段名称匹配的数组索引; this would allow you to add fields later by adding the appropriate field to the database and HTML without touching the code, and use array_keys() to retrieve the types submitted to allow seamless addition of types. 这将允许您稍后通过向数据库和HTML中添加适当的字段而无需触摸代码来添加字段,并使用array_keys()检索提交的类型以允许无缝添加类型。 As long as your validations are tight, this is probably the most automated way to allow future expansion... 只要您的验证很严格,这可能是允许将来扩展的最自动化的方法...

Ex. 防爆。 Field Names: 栏位名称:

contact[main][name]
contact[main][telephone]
...
contact[backup][email]

Ex. 防爆。 Code: 码:

foreach(array_keys($request->input('contact')) as $type) {
    $contact = Contact::create($request->input('contact.'.$type));
    $contact->type = $type;
    $contact->save();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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