如何在laravel中使用eloquent在数据透视表中显示值

时间:2016-05-09 11:04:32

标签: laravel laravel-5 eloquent laravel-5.1

我有这个注册项目,学生只会选择他/她想要注册的科目。这些主题将被自动获取并显示在桌面上。 See this image.学生点击注册表单的添加按钮后,主题将被添加到added subjects form.但是我有问题显示所有部分的值和主题在我的数据透视表上定义是section_subject。我怎样才能做到这一点?我已经在Section Model和Subject Model上定义了belongsToMany关系。这是我的代码。

Subject.php

class Subject extends Model
{
    public function sections()
    {
        return $this->belongsToMany(Section::class);
    }
}

Section.php

class Section extends Model
{
    public function subjects()
    {
        return $this->belongsToMany(Subject::class);
    }
}

ReservationController类

class ReservationController extends Controller
{
    public function create($id)
    {
        $subjects = Subject::with('sections')->get();

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

form.blade.php

<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>
  @foreach($subjects as $subject)
    <tr>
      <td>{{ $subject->section_code }}</td>
      <td>{{ $subject->subject_code }}</td>
      <td></td>
      <td></td>
      <td>{{ $subject->units }}</td>
      <td>Df</td>
      <td>
         <button class="btn btn-xs btn-primary">Add</button>
         <button class="btn btn-xs btn-info">Edit</button>
      </td>
    </tr>
   @endforeach

这是section_subject表

Schema::create('section_subject', function (Blueprint $table) {

  $table->integer('section_id')->unsigned();
  $table->integer('subject_id')->unsigned();

  $table->foreign('section_id')
        ->references('id')
        ->on('sections')
        ->onDelete('cascade');

  $table->foreign('subject_id')
        ->references('id')
        ->on('subjects')
        ->onDelete('cascade');

  $table->primary(['section_id', 'subject_id']);
});

此外,这里分别是主题表和节表。

Schema::create('subjects', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('department_id')->unsigned();
  $table->string('subject_code');
  $table->string('subject_description');
  $table->integer('units');
  $table->integer('cost');
  $table->timestamps();
});

Schema::create('sections', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('instructor_id')->unsigned();
  $table->string('section_code');
  $table->string('section_description');
  $table->string('room_no');
  $table->date('schedule');
  $table->date('semester');
  $table->timestamps();
});

2 个答案:

答案 0 :(得分:1)

尝试{{ $subject->pivot->section_code }}

另外,将此添加到关系:

->withPivot('section_code')

有关从数据透视表here获取数据的更多信息。

答案 1 :(得分:0)

如果我是对的,你想获得数据透视表中的所有主题和部分。如果是这种情况,那么您可以创建一个新模型SectionSubject并从中拉出不同的数据。

Model SectionSubject:     

use Illuminate\Database\Eloquent\Model;

class SectionSubject extends Model {

    public function section()
    {
        return $this->belongsTo(Section::class);
    }
    public function subject()
    {
        return $this->belongsTo(Subject::class);
    }
}

然后在您需要的Controller中将此模型定义为:

use App\SectionSubject;

最后,您可以将所有章节和主题查询为:

$sections = SectionSubject::distinct()->select('section_id')->with('section')->get();
$subjects = SectionSubject::distinct()->select('subject_id')->with('subject')->get();

这将为您提供数据透视表中的所有主题和部分。我希望它可以帮助你。