Shiva
731
4
Laravel

Saving twice to database

Posted 2 years ago by Shiva

I hope someone can please look at my code and tell me what I've done wrong. For some reason when I create a new database item it gets saved twice instead of once.

My create.blade.php

{{ Form::open(array('route' => 'content.store', 'class' => 'add-form')) }}
                {{ csrf_field() }}
                <div class="form">

                    <div class="menu_input">
                        <div>
                            {{ Form::label('menu_id', 'Menu') }}
                        </div>
                        <div>
                            {{ Form::select('menu_id', $menu_options, '', array("class" => "form-control")) }}
                        </div>
                    </div>
                        
                    <div class="title_input">
                        <div>
                            {{ Form::label('title', 'Title') }}
                        </div>
                        <div>
                            {{ Form::text('title','', array('id' => 'title', 'class' => 'form-control')) }}
                        </div>
                    </div>

                    <div class="content_input">
                        <div>
                            {{ Form::label('content', 'Content') }}
                        </div>
                        <div>
                            {{ Form::textarea('content','', array('id' => 'content', "class" => "form-control")) }}
                        </div>
                    </div>


                    <div class="image_uploader">
                        <div id="fileuploader" class="image_upload">Upload</div>
                        <script>
                            var uploadObj = $("#fileuploader").uploadFile(settings);
                        </script>
                        {{ Form::hidden('image', '', array('id' => 'img-add', 'class' => 'img-hidden')) }}
                    </div>

                    <div class="submit_button">
                        {{ Form::submit('Submit', array("class" => "btn btn-info submit", "role" => "button")) }}
                    </div>
                </div>
            {{ Form::close() }}

my store method in my ContentController.php

public function store(Request $request)
    {
        //Gets all the input in the fields in the form
        $input = Input::all();
        //Checks the input fields against the validation rules in the Content model
        $validation = Validator::make($input, Content::$rules);

        //If the validation fails the a message will pop up saying that there was validation errors
        if($validation->fails()){
            return redirect()->route('admin.content.create')
                ->withInput()
                ->withErrors($validation)
                ->with('message', 'There were validation errors');
        }

        //If the validation passes then it gets saved to the database
        if($validation->passes()){

            $content = new Content();

            //Gets the menu_id
            $menuId = (array) array_get($input, 'menu_id');
            //Saves the input to the database
            $content->fill($input)->save();
            //Syncs the menu function in the Content model to save the menu ID in content_menu
            $content->menu()->sync($menuId);

            $content = Content::all();

            return view('content::admin.index', compact('content'));
        }
    }

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