我正在尝试在Laravel 5.6中的3个模型之间建立联系
这是我的桌子
部门
ID
名称
科目
ID
名称
教师
ID
名称
教
id_teacher
id_department
id_subject
所有表之间的关系是多对多的。
老师可以在多个部门教授很多科目
部门属于许多学科
学科属于许多部门
如何在教师,部门和学科模型中建立这些关系?
参考方案
您可以尝试如下操作:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Department extends Model
{
/**
* The teachs that belong to the department.
*/
public function teachs()
{
return $this->belongsToMany('App\Teach', 'teach_department', 'department_id', 'teach_id');
}
}
class Subject extends Model
{
/**
* The teachs that belong to the subject.
*/
public function teachs()
{
return $this->belongsToMany('App\Teach', 'teach_subject', 'subject_id', 'teach_id');
}
}
class Teacher extends Model
{
/**
* The teachs that belong to the teacher.
*/
public function teachs()
{
return $this->belongsToMany('App\Teach', 'teach_teacher', 'teacher_id', 'teach_id');
}
}
class Teach extends Model
{
/**
* The departments that belong to the teach.
*/
public function departments()
{
return $this->belongsToMany('App\Department', 'teach_department', 'teach_id', 'department_id');
}
/**
* The subjects that belong to the teach.
*/
public function subjects()
{
return $this->belongsToMany('App\Subject', 'teach_subject', 'teach_id', 'subject_id');
}
/**
* The teachers that belong to the teach.
*/
public function teachers()
{
return $this->belongsToMany('App\Teacher', 'teach_teacher', 'teach_id', 'teacher_id');
}
}
php-casperjs获取内部文本 - php我正在为casperjs使用php包装器-https://github.com/alwex/php-casperjs我正在网上自动化一些重复的工作,我需要访问一个项目的innerText,但是我尚不清楚如何从casperjs浏览器访问dom。我认为在js中我会var arr = document.querySelector('label.input…
php:是否有充分的理由引用所有数组键/索引? - php我正在遍历别人的代码,他们总是避免转义其数组键。例如:$ row_rsCatalogsItems [名称]代替$ row_rsCatalogsItems ['名称']因此,我不断地对自己接触的所有事物进行微小的更改,以应对这些惰性。但是现在我想知道这样做是否有很多好处。我得到它会在默认为字符串之前检查常量(我在处理常量时会讨厌php中的行为,因为即使未定义,…
PHP标头功能不起作用 - php我曾经使用此功能从一个PHP页面重定向到另一个:header( 'Location: student.php?cnp='.$_REQUEST['name']) ; 在我的本地主机中,它确实可以工作,但是如果在Internet中对其进行测试,则不会重定向。我也尝试给出完整的路径(如http://.../student.p…
排序PHP迭代器 - php有没有一种简单的方法可以在PHP中对迭代器进行排序(而不仅仅是将其全部拉入数组并进行排序)。我有一个具体的例子是DirectoryIterator,但是对所有迭代器都有一个通用的解决方案会很好。$dir = new DirectoryIterator('.'); foreach ($dir as $file) echo $file->…
PHP getallheaders替代 - php我正在尝试从服务器上的apache切换到nginx。唯一的问题是我在PHP脚本中使用的getallheaders()函数,该函数不适用于Nginx。我已经尝试过用户在getallheaders函数上的php站点上提供的注释,但这并不返回所有请求标头。请告诉我如何解决这个问题。我真的想切换到Nginx。 参考方案 您仍然可以使用它,但是您必须像这里一样重新定义…