简体   繁体   English

Laravel 5.2如何通过laravel雄辩的模型连接三个表

[英]Laravel 5.2 How to join three table by laravel eloquent model

I have three model 我有三种模式

1. user 
2. course 
3. fee

where user model is connected with course table with one to many relationship and course model is connected with fees table with one to one relationship. 用户模型与课程表具有一对多的关系,而课程模型与费用表具有一对一的关系。

the problem is, I have to find sum of all fee's where user selected the course and there user's completed stauts should be 1. how to write the query for it? 问题是,我必须在用户选择课程的地方找到所有费用的总和,并且用户已完成的提示应该是1.如何编写查询?

Course Model: 课程模式:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $table='courses';
    protected $fillable = ['name','date','publication_status'];


    public function fee(){
        return $this->hasOne('App\Fee','course_id', 'id');
    }
      public function user()
    {
        return $this->belongsToMany('App\User','course_id','id');
    }
}

User Model: 用户模型:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $table='users';
     protected $fillable=['name','email','phone','course_id'];



          public function courses()
    {
        return $this->hasMany('App\Course','id','course_id');
    }
}

fee Model: 收费模式:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Fee extends Model
{
        protected $table='fees';
    protected $fillable = ['fee','course_id'];

    public function course()
    {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }
}

if anybody know the solutions, please provide one. 如果有人知道解决方案,请提供一个。

I guess you can find the user first: 我想您可以先找到用户:

if(User::find($id)->complete_status == 1){
    $user = User::find($id);
}

After this you will return all courses of this user: 之后,您将返回该用户的所有课程:

$courses = $user->courses;

And finally you'll implement a foreach statement to return each courses fee: 最后,您将实现一个foreach语句以返回每个课程费用:

foreach($courses as $course){
    $feeSum += $course->fee->value;
}

A example to pass your fee sum to view: 传递费用总额以查看的示例:

return view('view_path' , compact('feeSum'));

Suposing that $feeSum is in the same function that return view statement. 假设$ feeSum在返回视图语句的同一函数中。

After return the variable, in your view file you'll add this for example: 返回变量后,在视图文件中添加以下示例:

<label>{{ $feeSum }}</label>

You might want to look into using pivot-like relationships. 您可能想研究使用类似枢轴的关系。

Your User model could have something like: 您的用户模型可能具有以下内容:

# Get the fees for a user:
public function fees()
{
    return $this->hasMany('App\Fees', 'fees', 'courses', 'course_id', 'course_id')
                ->withPivot('iterate', 'fields', 'here', 'to', 'pass', 'on')
                ->withTimestamps();
}

The code above has not been tested at all, but I'm pretty sure the hasMany / belongsToMany methods allow for linking several tables in a relationship. 上面的代码根本没有经过测试,但是我很确定hasMany / EmiratesToMany方法允许链接关系中的多个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM