未定义索引Laravel控制器功能"存储"

时间:2017-04-25 08:20:52

标签: javascript php ajax laravel laravel-5

研究员,

我目前正在为我的实习生开发一个小时的注册系统。我现在遇到的问题是,当我向表格添加一个新行时,同事可以使用ajax添加他们的任务和时间。即使设置了变量,我也会得到一个未定义的索引错误(或者我想相信)。

这是我的Ajax / javascript代码,用于追加新行并使用ajax函数

的Ajax:       addRows ++;

  $(".btn-save").click(function(){

    rowId = $(this).attr('rel');

    var url = '/hoursregistrations/create';
    var data = {
        user_id: {{  $user_id->id }},
        project_id: $("tr#addRow-"+rowId + " .project_id").val(),
        note: $("tr#addRow-"+rowId + "  .note").val(),
        hours: $("tr#addRow-"+rowId + " .hours").val(),
        date: $("tr#addRow-"+rowId +  " .date").val(),
    };

    $.ajax({
        headers:{ 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        type: 'post',
        url: url,
        dataType: 'json',
        data: data,
        success: function(data) {
            console.log(data);
            $("table tbody").append(markup);
        //redirect
        if(type=="exit")
            window.location = "create.blade.php";
    }
});
});

});

});

附加:

     $(document).ready(function() {
addRows = 0;
$("#addField").click(function (){
  var user_id = $('#user_id').val();
  var project_id = $('#project_id').val();
  var note = $('#note').val();
  var date = $('#date').val();
  var hours = $('#hours').val();
  var markup = "<tr id='addRow-"+(addRows)+"'><td hidden>{{  $user_id->first_name }}</td></td>"
  + user_id + 
  " <td> <select class='project_id' value=''><option>{!! \App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</option></td>"
  + project_id +
  "<td><input type='text' value='{{ $hoursregistration->note }}' name='note' class='note'></td></td>"
  + note +   
  "<td><input type='date' class='date' value='{!! \App\Helpers::dateFormat($hoursregistration->date) !!}'></td>"
  + date +
  "<td><input type='hours' class='hours' value='{{ $hoursregistration->hours }}'></td></td>"
  + hours +
  "<td><button id='btn-save' name='btn-save' class='btn btn-default btn-xd btn-save save-hoursregistration' value='' rel='addRow-"+addRows+"'>Save</button>"
  $("table tbody").append(markup);

身份等等。很好,我只是无法在这里发布代码哈哈..

Store function in the Controller

public function store(Request $request)
{
    //$hoursregistration= new Hoursregistration($request->except(''));


    $input = Input::all();

    $hoursregistration = new Hoursregistration();
    $hoursregistration->user_id = Sentinel::getUser()->id;
    $hoursregistration->project_id = $input['project_id'];
    $hoursregistration->subproject_id = $input['subproject_id'];
    $hoursregistration->date = $input['date'];
    $hoursregistration->note = $input['note'];
    $hoursregistration->hours = $input['hours'];
           //   dd($hoursregistration); 
    $hoursregistration->save();
    return redirect('hoursregistrations')->with('success', Lang::get('message.success.create'));
}

有没有人知道如何摆脱我的未定义索引&#34; project_id&#34;错误?

提前致谢!

编辑:

应用程序的路径:

            Route::group(array('prefix' => 'hoursregistrations'), function () {
            Route::get('/', array('as' => 'hoursregistrations.index', 'uses' => 'HoursregistrationsController@index', function(){
                $projects = Project::all();
                return View::make('index')->with('hoursregistrations', $hoursregistrations);
            }));



            Route::get('data', ['as' => 'hoursregistrations.data', 'uses' => 'HoursregistrationsController@data']);

            Route::get('/create', array('as' => 'hoursregistrations.create', 'uses' => 'HoursregistrationsController@create'));
            Route::post('/create', 'HoursregistrationsController@store');
            Route::get('/show/{id}', array('as' => 'hoursregistrations.show', 'uses' => 'HoursregistrationsController@show'));
            Route::get('/edit/{id}', array('as' => 'hoursregistrations.edit', 'uses' => 'HoursregistrationsController@edit'));

            Route::post('/edit/{id}/update', array('as' => 'hoursregistrations.update', 'uses' => 'HoursregistrationsController@update'));
            Route::get('/{id}/delete', array('as' => 'hoursregistrations.delete', 'uses' => 'HoursregistrationsController@getDelete'));
            Route::get('/{id}/confirm-delete', array('as' => 'hoursregistrations.confirm-delete', 'uses' => 'HoursregistrationsController@getModalDelete'));
        });

HTML:

<div class="panel-body table-responsive">
            <!-- Tabel om de data te weergeven -->
            <table class="table table-bordered" id="myTable">
                <thead>
                    <tr class="filters"> 
                       <th>Titel Project</th>
                       <th>Notitie</th>
                       <th>Datum</th>
                       <th>Uren</th>
                       <th>Actions</th>
                   </tr>
               </thead>
               <tbody id="content">
                @foreach ($hoursregistrations as $hoursregistration)
                <tr>
                   <td hidden>{{  $user_id->first_name }}</td>
                   <td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>      
                   <td>{!! $hoursregistration->note !!}</td>    
                   <td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
                   <td>{!! $hoursregistration->hours !!}</td>
                   <td>
                    <button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
                    <button id="btn-delete" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
                    <button id="btn-save" name="btn-save" class="btn btn-default btn-xd btn-save save-hoursregistration" value="">Save</button>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    <!-- End: tabel die data weergeeft -->
</div>

1 个答案:

答案 0 :(得分:1)

使用AJAX发布的数据对象中的键必须有引号,如果你要像他们那样调用它们。

var data = {
    'user_id': {{  $user_id->id }},
    'project_id': $("tr#addRow-"+rowId + " .project_id").val(),
    'note': $("tr#addRow-"+rowId + "  .note").val(),
    'hours': $("tr#addRow-"+rowId + " .hours").val(),
    'date': $("tr#addRow-"+rowId +  " .date").val(),
};

或者,您必须使用JSON.stringify

将数据转换为JSON字符串作为请求的一部分
$.ajax({
    headers:{ 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    type: 'post',
    url: url,
    dataType: 'json',
    data: JSON.stringify(data),
    success: function(data) {
        console.log(data);
        $("table tbody").append(markup);
    //redirect
    if(type=="exit")
        window.location = "create.blade.php";
}