gpt4 book ai didi

php - 如何使用多个输入更新模型列

转载 作者:行者123 更新时间:2023-11-29 03:03:59 25 4
gpt4 key购买 nike

我正在处理一个 Laravel 4 项目,我正在尝试向一个简单服务表中的服务列添加一堆服务。列的格式为:

id   userID   services   price

我希望它做的是,当用户选择服务时,用户选择的所有服务都将放入服务列中,如下所示:

id   userID   services   price
1 5 clean $4.95
2 5 unload $7.95
3 5 dance $1.95
4 5 vacuum $12.95
5 5 clean $4.95

基本上所有的服务都在不同的输入字段中。所以他们没有关系。

我尝试向模型添加一个数组,但出现错误:

Array to String conversion

我的 Controller 的一部分

$service = new Service();

$service->userID = $user->id;
$service->services = array(
Input::get('rooms'),
Input::get('pr_deodorizer'),
Input::get('pr_protectant') .
Input::get('pr_sanitzer'),
Input::get('fr_couch'),
Input::get('fr_chair'),
Input::get('fr_sectional'),
Input::get('fr_ottoman')
);

var_dump($service->services);die;

$service->save();

最佳答案

Laravel 不会理解您想从数组创建模型的各种新实例,因此对于每个服务,您必须为每个服务模型创建一个单独的实例,例如。

$service = new Service();

$service->userID = $user->id;
$service->services = Input::get('rooms');

$service->save();

一个想法.. 也许您可以使用方括号表示法将您的服务分组到表单上的 PHP 数组中:

<div>
<label for="room">Room</label>
<input type="text" id="room" name="services[]">
</div>
<div>
<label for="pr_deodorizer">Deodorizer</label>
<input type="text" id="pr_deodorizer" name="services[]">
</div>
<div>
<label for="pr_protectant">Protectant</label>
<input type="text" id="pr_protectant" name="services[]">
</div>

然后循环遍历 Controller 中的选定服务:

foreach(Input::get('services') as $service)
{
$service = new Service();

$service->userID = $user->id;
$service->services = $service;

$service->save();
}

...那没有经过测试,我不知道你的数据格式是什么,但应该让你开始解决这个问题..

关于php - 如何使用多个输入更新模型列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18678444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com