【问题标题】:How can i get data using Eloquent Relationships in laravel如何在 laravel 中使用 Eloquent 关系获取数据
【发布时间】:2020-11-07 23:42:18
【问题描述】:

我需要根据teacher表数据从teaching_subjects表中获取数据到我的Vue文件中。我已经创建了与 TeachingSubject 模型和 Teacher 模型的关系。

这是我的教师模型,

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Teacher extends Model
    {
        protected $table = 'teacher';
        protected $fillable = [
            'firstName',
            'lastName',
            'teacherUserName',
            'contactNo',
            'teacherEmail',
            'gender',
            'teaching_expirence',
            'Qualifications',
            'description',
            'speakEnglish',
            'speakJapan',
            'password',
            'status',
        ];
    
        public function approvalStatus(){
            return $this->belongsTo(ApprovalStatus::class);
        }
        
        public function teachingSubject(){
            return $this->hasMany(Teacher::class);
        }
    }

这是我的教学主题模型

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class TeachingSubject extends Model
    {
        protected $table = 'teaching_subjects';
        protected $fillable = [
            'teacher_id',
            'subject_id',
            'lesson_fee'
    
        ];
        
        public function teacher(){
            return $this->hasMany(Teacher::class);
        }
        public function subject(){
           return hasMany(Subject::class);
        }
    }
This is my Subject model,

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Subject extends Model
    {
        protected $table = 'subject';
        protected $fillable = [
            'subject',
            'grade_id',
            'no_of_teachers',
            'status'
    
        ];
        public function teachingSubject(){
            return belongsTo(TeachingSubject::class);
        }
    }

这是我的 Teching_subjects 迁移文件

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateTeachingSubjectsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('teaching_subjects', function (Blueprint $table) {
                $table->integer('id')->unsigned()->autoIncrement();
                $table->integer('teacher_id')->unsigned();
                $table->integer("subject_id")->unsigned();
                $table->float("lesson_fee")->nullable();
                $table->timestamps();
    
                $table->foreign('teacher_id')
                    ->references('id')->on('teacher');
    
                $table->foreign('subject_id')
                    ->references('id')->on('subject');
    
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('teaching_subjects');
        }
    }

我需要使用 TeachingSubjectController 获取日期(created_at)、教师姓名、课程和课程费用。

【问题讨论】:

  • 看来你需要一个带有数据透视表的manyToMany relationship
  • Teacher 类中,为什么要使用Teacher::Class 来表示它的hasMany 关系?

标签: php laravel eloquent eloquent-relationship


【解决方案1】:

例如,如果您想获取发表评论的用户,请从评论对象中获取该用户。

试试这个

$user = User::find(1);
$posts = $user->posts;

foreach ($posts as $post) {
    foreach ($post->comments as $comment) {
        echo $comment->commentcontent;
        echo $comment->users;
    }
}

【讨论】:

  • 我在控制器公共函数 getTeachingSubjects(){$teachingSubjects = TeachingSubject::all();return response()->json($teachingSubjects,200)} 中创建了方法,但只提供包含的日期教学科目表
猜你喜欢
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2017-07-07
  • 1970-01-01
  • 2014-10-29
  • 1970-01-01
  • 2017-05-01
  • 2021-12-10
相关资源
最近更新 更多