Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

zaki's avatar
Level 1

Save multiple data in database

Hy everyone, i had two fields like this and later i needed to repeat those fields so i did that with JavaScript like this


                        <div class="col-md-9">
                            <div class="form-group">
                                {{Form::label('_customer_name', 'Customer Name :', ['class' => 'contc p-4 icn']) }}
                                {{Form::email( '_customer_name', null, ['class' => 'form-control chos_bsns', 'id' => '_customer_name', 'placeholder' => 'eMail here ']) }}
                            </div>
                        </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                
                                {{sprintf(Form::label('_feedback', '%s',['class' => 'compny_over icn']),'Feedback recieved from customer &nbsp;&nbsp;<small>( Maximum 100 words )</small>
                                <span> Word Count : 6 </span>') }}
                                {{Form::textarea( '_Feedback', null, ['class' => 'form-control', 'id' => '_feedback', 'placeholder' => 'Information regarding service' , 'rows' => '5']) }}
                              </div>
                            </div>
                    </div>
                        {{Form::submit('Add More', ['class' => 'p3_sbmit btn btn-default primary pull-left', 'id' =>'repeat'])}}

and i was submitting the values in the data base like this earlier

            [

               'name'=>Input::get('_customer_name'),
               'feedback'=>Input::get('_Feedback'),
               'website_id'=>Input::get('website_id')
                            
            ];

Testimonial::create($data);

but since i repeated it i dont know to store those in the database any ideas would be much help.

0 likes
25 replies
d3xt3r's avatar

use array as input field, like _customer_name[], in your controller loop through them and create the Testimonial.

1 like
d3xt3r's avatar

Note: just an example, not a solution.

 {{Form::email( '_customer_name[]', null, ['class' => 'form-control chos_bsns', 'id' => '_customer_name_1', 'placeholder' => 'eMail here ']) }}
// Hardcoded or add dynamically
 {{Form::email( '_customer_name[]', null, ['class' => 'form-control chos_bsns', 'id' => '_customer_name_2', 'placeholder' => 'eMail here ']) }}

In controller

$emails = Request::get('_customer_name');
foreach($emails as $email) {
    // Do something
}
Ftoi's avatar

can you explain more bro , i didn't Get your ideas :)

1 like
zaki's avatar
Level 1

i repeated some fields with javascript now i am getting an array and i want to store it into the database . :)

Ftoi's avatar

$imploarray= implode(',',array_values($array)); $query = DB::query("insert into customer (id, name, feedback, blabla) VALUES($imploarray)"); if doesn't work give me a example of your array :)

1 like
zaki's avatar
Level 1

@Ftoi with that method all the data will be stored in a single row in the database. Buddy I am getting an array like this

$emails = Request::get('_customer_name');
    $feedbacks = Request::get('_Feedback');
    var_dump($emails);
    var_dump($feedbacks);
array(2) { [0]=> string(14) "test@email.com" [1]=> string(15) "test1@email.com" } array(2) { [0]=> string(12) "1st feedback" [1]=> string(12) "2nd feedback" } 

in which test@email.com gives the feedback value which is "1st feedback" and similarly the test1@email.com submits the value 2nd feedback.

d3xt3r's avatar

@zaki Did you try what I suggested with the foreach loop ?

1 like
Ftoi's avatar

my ideas is to put a loop for begin from $x=0 to count of array than into for() stock $value=array[$x] than put insert for this and empyt the $value
now every value stock in his row trying this ideas hope working for you

1 like
zaki's avatar
Level 1

@premsaurav i tried but i have two values one is emails and the second one is feedbacks , so i need a second loop for that? i am sorry i did not understood it thoroughly.

d3xt3r's avatar
d3xt3r
Best Answer
Level 29

No, assuming you are consistently creating the fields using js, i.e number of emails = number of feedbacks

for($ii = 0; $ii < count($emails) ; $ii++) {
    $email = $emails[$ii];
    $feedback = $feedbacks[$ii];
    Testimonial::create([
        'name' => $email,
        'feedback'=>$feedback
    ]);
}
2 likes
zaki's avatar
Level 1

@premsaurav it worksssss, have i told you how much of a great help you become today. :) thank you so much. but what if emails are not equals to no of feedbacks ? is there any other way.? Much appreiciated all of you. :D

d3xt3r's avatar

What do you want to achieve, when you say no of emails not equal to number of feedbacks. May be if you can explain the use case verbally we may be able to propose a solution.

zaki's avatar
Level 1

hey @premsaurav now can i get a way to update these fields ? here is what i have done 1- fetch all the data from the database and displayed it in the fields

$information= Service::where('website_id', '=', $id)->get();
and used a foreach loop to show all the data and now i am troubling to update it.

any help? :)

d3xt3r's avatar

What trouble and how have you implemented it as of now ?

zaki's avatar
Level 1

@premsaurav now that i am trying to update it like this

for($ii = 0; $ii < count($infos) ; $ii++) 
        {
        $info = $infos[$ii];
        $price = $prices[$ii];
        $intro = $intros[$ii];
        $popular = $populars[$ii];
        $category = $categories[$ii];
$data = 
         [
        'name' => $info,
        'price'=>$price,
        'description'=>$intro,
        'service_category_id'=>$category,
        'popular'=>$popular,
        ];
        
        if(isset($path))
        $data['document'] = $path;
    
        $data =array_filter($data , 'strlen');
        
    $website->service->update($data);

but is giving an error that is

Call to undefined method Illuminate\Database\Eloquent\Collection::update()

d3xt3r's avatar

May be there's better way but you don't need to update it using relationship.

Obviously,

 $website->service->update($data); // wont work as it returns a collection not a model or a builder.

You could simply use

Service::where('id',$serviceId)->update($data); // If not using pivot table

If using pivot table, look at sync method.

1 like
zaki's avatar
Level 1

@premsaurav is there any way i can contact u personally? :) you seem really helpful and i really need it.

Please or to participate in this conversation.