2

I am working on a Laravel 4 project and I am trying to add a bunch of services to the services column in a simple service table. the columns are formatted as:

id   userID   services   price

What I would like it to do is when a user selects the services all the services that user selects get's put into the services column to look like:

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

basically all the services are in different input fields. So they aren't related.

I tried adding an array to the model but it gives an error:

Array to String conversion

Part of my 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();
4

1 回答 1

1

Laravel won't understand that you want to create various new instances of the model from just the array, so for each of the services, you'll have to create a separate instance of the Service model for each, eg.

$service = new Service();

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

$service->save();

One idea.. perhaps you could group your services into a PHP array on your form using the square bracket notation:

<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>

and then loop through the selected services in your Controller:

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

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

    $service->save();
}

... That's not tested, and I don't know what the format of your data is, but should give you a start on the problem..

于 2013-09-08T00:06:30.637 回答