1

I'm Using Laravel 4 to make an application. There is a form to create an event, which is then processed in the Controller.

This is my code for the checkboxes:

@foreach($value['value'][$i] as $key1=>$value1)
    {{ Form::checkbox('amenities[]', $value1) }}
@endforeach

I have a function in my Controller, which has this code:

$amenitiesALL = array();
$amenitiesALL = Input::get('amenities');
foreach($amenitiesALL as $amenities)
{
    $amenity = new EventAmenities;
    $amenity::create(array('EVENT_ID' => $eventID, 'EVENT_AMENITIES_ID' => $amenities));
}

It's raising an error every time [2014-06-29 13:55:46] production.ERROR: exception 'ErrorException' with message 'preg_replace(): Parameter mismatch, pattern is a string while replacement is an array' in E:\wamp\www\bt\vendor\laravel\framework\src\Illuminate\Support\helpers.php:973

How do I make the variable take the input from the form as an array? Any other suggestions would also help.

6
  • What does your model look like. EVENT_AMENITIES_ID is a column that refers to another table/model? Commented Jun 29, 2014 at 14:28
  • @Unnawut I have tables USER_EVENTS and AMENITIES, AND EVENT_AMENITIES is a pivot table. And yes, foreign keys are in play referencing both tables. I'll let you know the flow, which now that i think of it, is important to know. I store the input of amenities (checkboxes) in a variable (i think array), and fetch the event_id from USER_EVENTS, and then for each amenity, i insert the event_id and the amenity in the EVENT_AMENITIES table. Should that pose any problem? Commented Jun 29, 2014 at 14:38
  • Can you check that $eventID and $amenities are already integers? Or a stacktrace from your app/storage/logs/laravel.log would also help. I don't know why it's trying to call preg_replace(). Model::create() doesn't seem to need a preg_replace. Commented Jun 29, 2014 at 15:31
  • @Unnawut I did $amenities = ''.$ameneties.''; And then it worked! Commented Jun 29, 2014 at 15:39
  • That's interesting. Do post as an answer. Would be nice if you could tell us what you get when you var_dump($ameneties) as well. Commented Jun 29, 2014 at 15:44

1 Answer 1

1

Change

$amenity = new EventAmenities;
$amenity::create(array('EVENT_ID' => $eventID, 'EVENT_AMENITIES_ID' => $amenities));

To

$amenity = EventAmenities::create(array('EVENT_ID' => $eventID, 'EVENT_AMENITIES_ID' => $amenities));

Or if you want to store it, declare $amenity = array() outside the loop and change the above to

$amenity[] = EventAmenities::create(array('EVENT_ID' => $eventID, 'EVENT_AMENITIES_ID' => $amenities));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.