dosrmo0442
2018-07-03 16:22
浏览 170
已采纳

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

I need to be able to loop through a list of laravel request variables and do something with them. 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?

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.

图片转代码服务由CSDN问答提供 功能建议

我需要能够循环遍历laravel请求变量列表并对它们执行某些操作。 我希望能够在调用请求对象时使用变量,以便我可以在循环中运行它而不是为每个变量编写一行代码。

例如,我的文本 输入可能具有类似的名称

  contact_main_name 
contact_main_telephone 
contact_main_email 
 
contact_sub_name 
contact_sub_telephone 
contact_sub_email 
 
 ncontact_backup_name 
contact_backup_telephone 
contact_backup_email 
    
 
 

在我的请求中,我不想写

  $ request-> contact_main_name 
 $ request-&gt  ; contact_main_telephone 
   
 
 

对于我可能拥有的每种不同类型的联系人,我希望能够像这样循环遍历它们

  $ contactTypes = [
'main',
'sub',
'backup',
'head'
]; 
 
 foreach($ contactTypes as $ type){
  //开始新联系人
 $ contact = new联系人; 
 $ contact-> type = $ type; 
 $ contact-> name = $ request-> $ {“contact _”。$ t  ype。“_ name”}; 
 $ contact-> telephone = $ request-> $ {“contact _”。$ type。“_ telephone”}; 
 $ contact-> email = $ request-> $  {“contact _”。$ type。“_ email”}; 
 $ contact-> save(); 
} 
   
 
 

我如何使用变量 调用laravel $ request 时的名称,以便我可以构建一个可能类型的数组并循环遍历它们?

注意 我知道我可以编辑输入字段本身看起来像 name =“contact [type] [name]”然后循环它们,但我不能改变输入名称,我必须 通过控制器本身的php来实现。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doutun1875 2018-07-03 16:28
    已采纳

    As answered in comments, to do this, change the method of calling the input and use the actual input() function itself.

    $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();
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dte66654 2019-03-22 16:29

    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. 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();
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题