簡體   English   中英

從laravel中的數據透視表訪問數據

[英]accessing data from a pivot table in laravel

我無法從數據透視表中的額外列訪問數據。

我有兩個模型(Grado和Subject)(這是一個用於管理k-12學校成績的較大應用程序的一部分)。 Grado是指年級,即一年級,二年級等。我希望管理員能夠將科目與給定年份中的年級相關聯。

mysql樞軸表grado_subject包含:

  • id_grado_subject

  • year_grado_subject //年份-我要訪問的列

  • grados_id_grados // grado的外鍵ID

  • subject_id_subjects //主題的外鍵ID

  • created_at

  • Updated_at

Grado模型:

<?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相關的所有年份時,出現錯誤:試圖獲取非對象的屬性。

任何幫助將不勝感激!

公共函數subject(){return $ this> belongsToMany('Subject','grado_subject','grados_id_grados','subjects_id_subjects')-> withPivot('year_grado_subject')-> withTimestamps(); }

您在上面看到了一些語法錯誤。 首先你有$ this> belongsToMany應該是$ this-> belongsToMany

您可能要檢查的另一件事是$ this-> belongsToMany('Subject',...)您沒有在use子句中包含Student模型,因此您必須使用完全合格的引用,並且應該是$ this- > belongsToMany ('\\ Student',...)

您在Grado模型中有相同的語法錯誤。 首先檢查這些,看看您的錯誤是否已解決。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM