mardi 19 juin 2018

laravel forms inserting and updating

I'm using the same form for updating and inserting

When Im inserting data i have no errors but when updating it I need to use

<input type="hidden" name="_method"  id="_method" value="PUT">

The form is

× Modal Header has('heading') ? ' has-error' : '' }}"> Heading @if ($errors->has('heading')) @endif has('post') ? ' has-error' : '' }}"> Post @if ($errors->has('post')) @endif Submit Clear Close

And the view is

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">Posts
                        <span class="pull-right" data-toggle="modal" data-target="#myModal"><i class="fa fa-plus" data-toggle="tooltip" title="Add new post"></i></span>
                    </div>

                    <!-- panel body -->
                    <div class="panel-body">
                    @foreach($posts as $post)
                            <div class="panel panel-primary post-container">    <!-- post-container is the parent -->
                                <div class="panel-heading"><span class="post-heading"></span>

                                    @if($post->users_id==\Illuminate\Support\Facades\Auth::id())
                                        <span class="pull-right post-delete" >
                                        <form action="" method="POST" style="" onsubmit="return confirm('Are you sure?');">
                                            <input type="hidden" name="_method" value="DELETE">
                                            
                                            <button class="btn btn-danger btn-sm">Delete</button>
                                        </form>
                                        </span>
                                    @endif


                                    @if($post->users_id==\Illuminate\Support\Facades\Auth::id())
                                        <span class="pull-right post-edit" style="margin-right: 10px; cursor: pointer;" data-id=""  data-toggle="modal" data-target="#myModal"><button class="btn btn-success btn-sm">Edit</button></span>
                                    @endif  <!-- post-edit is the name of the edit section -->

                                </div><!-- end of panel heading -->

                                <div class="panel-body">
                                    <p>Created by  at </p>
                                    <hr/>
                                    <span class="post-body"> </span>
                                </div><!-- end of panel body -->

                            </div><!-- panel container -->

                        @endforeach


                    </div>
                    <!-- end of panel body -->

                </div>
            </div>
        </div>
    </div>
    @include('post.form');

    @endsection

    @section('js')
    <script>
        $('.post-edit').click(function(){

            $('#heading').val($(this).parents('.post-container').find('.post-heading').text());
            $('#post').val($.trim($(this).parents('.post-container').find('.post-body').text()));
            $('#form_post').attr('action','post/'+$(this).data('id'));
            $('#_method').val("PUT");

        });
    </script>

@endsection

and now by using

<input type="hidden" name="_method"  id="_method" value="PUT">

I can't insert data i end up getting an error saying

No message



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire