3 months ago

fake relation when form fails to submit a repeater fields

Posted 3 months ago by RFFRED


I am trying to build a form that can handle repeater fields. I am no expert and this is bit hard to explain, but in essence I have a page model linked to a template model and my templates have allocated modules.

I have a testimonial module made of a 2 models: TestimonialRepeaters and testimonials

I have it all working but it feels a bit clumsy.

template model

public function init(Array $page_data)

        //creates a row for the page in testimonialrepeater table
        $obj = TmTestimonialRepeater::firstOrCreate(['page_id' => $page_data['page']['id']]);

    //loads the testimonials from the database
        $data['testimonialsRepeaters'] = $obj::with(['hasTestimonials'])->get();

        //accessing the current request via the request() helper 
        $request = request();

    //gets form errors, if any
        $errors = \Session::get('errors');
        if (is_null($errors)) {

            //collects the records related to the page we edit
        //$page_data['page'] is the page object we edit
            $data['testimonials'] = $page_data['page']->Testimonials()->get();

        //else if the page is posted AND has errors
        } else {

            //collects fields from form
            $ids = old('testimonial_id');
            $comments =old('testimonial_comment');

            $testimonials = array_map(null, $ids, $comments);

            //creates a collection of records from an array
            $collection = collect([]);

            foreach($testimonials as $testimonial)
                $model = new TmTestimonial;
                $model->id = $testimonial[0];
                $model->comment = $testimonial[1];

                //append model to collection
                $collection[] = $model;

            $data['testimonialsRepeaters'] = $obj::with(['hasTestimonials' => $collection]);


        $data['config'] = \Config::get('tm-testimonials');

        return $data;



@foreach ($page_data['modules_data']['tm-testimonials']['testimonialsRepeaters'] as $testimonialRepeater)
            {!! Form::text('testimonials_title', $testimonialRepeater->title, ['class' => 'form-control', 'placeholder' => 'title']) !!}

            @foreach ($testimonialRepeater->hasTestimonials as $testimonial)

                      {!! Form::hidden('testimonial_id[]', $testimonial->id ) !!}
                      {!! Form::text('testimonial_comment[]', $testimonial->comment, ['class' => 'form-control', 'placeholder' => 'Comment']) !!}
                    <td><button type="button" name="remove" class="btn btn-danger remove">Remove</button></td>



The line I am struggling with is in the template model

$data['testimonialsRepeaters'] = $obj::with(['hasTestimonials' => $collection]);

When the form fails to submit, I want compile the testimonials posted from the form and want to add them in the "hasTestimonials" relation.

Is there a way to do this?

Please sign in or create an account to participate in this conversation.