laravel - 使用 vue js 和 laravel 添加表数据时获取未定义的值

标签 laravel laravel-5 laravel-5.1 vue.js vue-resource

我有一个项目,当用户点击一个按钮时,数据应该显示在一个表格上。但是,我使用的是此处所示的表格 http://i.stack.imgur.com/HW4rE.jpg .我想要的是,当用户点击注册表单上的添加按钮时,它应该显示在添加的主题表上,而不需要刷新页面。问题是我正在使用一个表,我无法使用 v-model 来绑定(bind)值。

这是我的 form.blade.php

报名表格

<table class="table table-bordered">
  <tr>
    <th>Section</th>
    <th>Subject Code</th>
    <th>Descriptive Title</th>
    <th>Schedule</th>
    <th>No. of Units</th>
    <th>Room</th>
    <th>Action</th>
  </tr>
  <body>
    <input type="hidden" name="student_id" value="{{ $student_id }}">
    @foreach($subjects as $subject)
      @foreach($subject->sections as $section)
      <tr>
        <td>{{ $section->section_code }}</td>
        <td>{{ $subject->subject_code }}</td>
        <td>{{ $subject->subject_description }}</td>
        <td>{{ $section->pivot->schedule }}</td>
        <td>{{ $subject->units }}</td>
        <td>{{ $section->pivot->room_no }}</td>
        <td>
          <button 
              v-on:click="addSubject( {{ $section->pivot->id}}, {{ $student_id}} )" 
              class="btn btn-xs btn-primary">Add
          </button>

          <button class="btn btn-xs btn-info">Edit</button>
        </td>
      </tr>
      @endforeach
    @endforeach
  </body>
</table>

AddedSubjects 表

<table class="table table-bordered">     
    <tr>
      <th>Section Code</th>
      <th>Subject Code</th>
      <th>Descriptive Title</th>
      <th>Schedule</th>
      <th>No. of Units</th>
      <th>Room</th>
      <th>Action</th>
    </tr>

    <body>

    <tr v-for="reservation in reservations">
      <td>@{{ reservation.section.section_code }}</td>
      <td>@{{ reservation.subject.subject_code }}</td>
      <td>@{{ reservation.subject.subject_description }}</td>
      <td>@{{ reservation.schedule }}</td>
      <td>@{{ reservation.subject.units }}</td>
      <td>@{{ reservation.room_no }}</td>
      <td>
        <button class="btn btn-xs btn-danger">Delete</button>
      </td>
    </tr>

    </body>
</table>

这是我的 all.js

new Vue({
el: '#app-layout',

data: {

    reservations: []

},
ready: function(){
    this.fetchSubjects();
},
methods:{

    fetchSubjects: function(){
        this.$http({
            url: 'http://localhost:8000/reservation',
            method: 'GET'
        }).then(function (reservations){
            this.$set('reservations', reservations.data);
            console.log('success');
        }, function (response){
            console.log('failed');
        });
    },

    addSubject: function(section,subject,student_id){
        var self = this;
        this.$http({
            url: 'http://localhost:8000/reservation',   
            data: { sectionSubjectId: section.pivot.id, studentId: student_id },
            method: 'POST'
        }).then(function(response) {
            self.$set('reservations', response.data);   
            console.log(response);
            self.reservations.push(response.data);
        },function (response){
            console.log('failed');
        });
    }
  }
});

这是我的 ReservationController

class ReservationController extends Controller
{

public function index()
{
    $student = Student::with(['sectionSubjects','sectionSubjects.section', 'sectionSubjects.subject'])->find(1);

    return $student->sectionSubjects;    

}

public function create($id)
{
    $student_id = $id;

    $subjects = Subject::with('sections')->get();

    return view('reservation.form',compact('subjects','student_id'));
}

public function store(Request $request)
{

    $subject = SectionSubject::findOrFail($request->sectionSubjectId);

    $student = Student::with(['sectionSubjects','sectionSubjects.section', 'sectionSubjects.subject'])->findOrFail($request->studentId);

    $subject->assignStudents($student);

    return $student->sectionSubjects;

   }
}

任何人都可以建议我如何解决这个问题,而不需要刷新页面。

顺便说一句,我的 console.log 中有这个错误

Error when evaluating expression "reservation.section.section_code": TypeError: Cannot read property 'section_code' of undefined

Error when evaluating expression "reservation.subject.subject_code": TypeError: Cannot read property 'subject_code' of undefined

Error when evaluating expression "reservation.subject.subject_description": TypeError: Cannot read property     'subject_description' of undefined

Error when evaluating expression "reservation.subject.units": TypeError: Cannot read property 'units' of undefined

最佳答案

从服务器获取数据后,您应该使用 vue-devtools 检查 reservations 中实际包含哪些数据。

因为错误消息只是表示此数组中的对象没有 .subject 属性,这意味着您收到的数据可能没有您假设的结构,并且是找到您的解决方案的地方。

我不太了解 PHP 或 Laravel,但您的 GET 请求似乎从 index 方法获得响应,并且此方法似乎以主题数组而不是保留数组(应包含主题,根据您的模板代码)

关于laravel - 使用 vue js 和 laravel 添加表数据时获取未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37429575/

相关文章:

php - 将 Laravel 模型扩展多于一层是一种好习惯吗?

mysql - 如何将自定义查询转换为 Laravel 5?

php - 谷歌应用引擎 : WARNING: The Cloud SDK no longer ships runtimes for PHP 5. 4

laravel - 预期响应代码 235 但得到代码 535 laravel 使用 office365 smtp 发送电子邮件

php - Insert Collection to saveMany Eloquent

Laravel 如何在 session 中存储额外的数据/值 Laravel

javascript - 当我回显 id 但当我回显名称时,带有 laravel 的 ajax 返回未定义

laravel - 如何从请求中的数组中添加/删除元素

php - Laravel 5 上的 Compiled.php 中的 InvalidArgumentException

php - Laravel 5.1 - 使用 where Eloquent 查询数据透视表的字段