从laravel中的数据透视表访问数据

时间:2016-03-26 01:26:44

标签: php laravel

我无法从数据透视表中的额外列访问数据。

我有两个模型(这是一个用于管理k-12学校成绩的更大应用程序的一部分) - Grado和Subject。 Grado是指成绩,即一年级,二年级等。我希望管理员能够将科目与特定年份的年级相关联。

mysql数据透视表grado_subject包含:

  • id_grado_subject

  • year_grado_subject // year - 我要访问的列

  • grados_id_grados // grado的外键ID

  • subjects_id_subjects //主题的外键ID

  • created_at

  • 的updated_at

格拉多模型:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Grado extends Eloquent implements UserInterface, RemindableInterface {

        use UserTrait, RemindableTrait;


          protected $table = 'grados';
          protected $primaryKey = "id_grados"; 
          protected $fillable = array('name_grados');


           public static $rules = array(
            );

                  public function subject()
              {
                  return       $this->belongsToMany('Subject','grado_subject','grados_id_grados','subjects_id_subjects')
              ->withPivot('year_grado_subject')
              ->withTimestamps();
              }

           public function groups()
           {
               return $this->hasMany('Group','id_grados','grados_id_grados');
           }

    }

主题模型:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Subject extends Eloquent implements UserInterface, RemindableInterface {
            use UserTrait, RemindableTrait;

            protected $table = 'subjects';
            protected $primaryKey = "id_subjects"; 


               protected $fillable = array('engSpanExtra_subjects', 'name_subjects','IntGrade_subjects');


            public static $rules = array(
               'name_subjects' => 'required',   
               'engSpanExtra_subjects' => 'required',
               'IntGrade_subjects'=> 'required'

           );
        public function grado()
        {
            return $this->belongsToMany('Grado','grado_subject','subjects_id_subjects','grados_id_grados')
               ->withPivot('year_grado_subject')
               ->withTimestamps();
        }

         public function teacher()
         {
              return $this->belongsToMany('Teacher','subject_teacher','subjects_id_subjects','teachers_id_teachers');

         }

}

在GradoController中:

class GradoController extends \BaseController {


    public function index()
    {

        $grados = Grado::find(1);
        $years = $grados->pivot->year_grado_subject;// line I get error on
        return $years;
        $grados->toarray();


        return View::make('grados.index',compact('grados', 'years'));
}

当我尝试检索与grade_subject相关的所有年份时,我收到错误:尝试获取非对象的属性。

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:0)

  

公共职能科目()                 {                     返回$ this&gt; belongsToMany(&#39;主题&#39;,&#39; grado_subject&#39;,&#39; grados_id_grados&#39;,&#39; subjects_id_subjects&#39;)                  - &GT; withPivot(&#39; year_grado_subject&#39;)                  - &GT; withTimestamps();                 }

我在上面看到了一些语法错误。首先你有 $ this&gt; belongsToMany ,它应该是 $ this-&gt; belongsToMany

你可以检查的另一件事是 $ this-&gt; belongsToMany(&#39; Subject&#39;,...)你没有在use子句中包含你的Student模型因此,您必须使用完全限定的参考,它应该是$ this- &gt; belongsToMany(&#39; \ Student&#39;,...)

Grado模型中的语法错误相同。首先检查这些,看看你的错误是否已经解决。

答案 1 :(得分:0)

pivot属性由belongsToMany关系生成,因此它存在于该关系(主题)加载的对象上,而不是主要对象(grado)。

例如:

// primary object is grado
$grado = Grado::find(1);

// loop through related subjects
foreach($grado->subject as $subject) {
    // pivot attribute exists on related subject
    echo $subject->pivot->year_grado_subject;
}

或者,如果你采取相反的方式:

// primary object is subject
$subject = Subject::find(1);

// loop through related grados
foreach($subject->grado as $grado) {
    // pivot attribute exists on related grado
    echo $grado->pivot->year_grado_subject;
}