【发布时间】:2019-11-24 02:30:30
【问题描述】:
假设我有这个数据库结构:
Table A:
-id
-user_id
Table B:
-id
-user_id
Table C:
-id
-user_id
Users:
-id
这是我的用户模型
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
protected $fillable = ['name','email','username','password'];
/**
* Relationships
*/
public function tableA()
{
return $this->hasOne(\App\Models\TableA::class);
}
public function tableB()
{
return $this->hasOne(\App\Models\TableB::class);
}
public function tableC()
{
return $this->hasOne(\App\Models\TableC::class);
}
public function tableD()
{
return $this->hasOne(\App\Models\TableD::class);
}
}
这就是我想要实现的目标:
public function tables()
{
// return all records from all 3 tables (Table A, Table B, Table C)
}
我可以通过在 User 模型中定义一个关系来获取有关表的特定信息的方法,说它有 TableA、TableB 等的一条记录。但是,我正在寻找一种方法来查找其中的哪一个表 (a,b,c,d) 有一个用户的 Fk,遵循相同的关系概念。
注意:在这种情况下,多态不是解决方案
【问题讨论】:
-
你为这个问题做了什么?
-
请添加更多上下文 - 特别是,这个问题到底与 Laravel 有什么关系?
-
@BhargavChudasama 直到现在,我正在建立单一的关系并给他们打电话,但我正在寻找一种简单而有效的方法来实现这一点,一种综合的方式。
-
@NicoHaase 它是一个具有雄辩关系的 laravel 项目,为什么这与 laravel 无关?我应该用 node js 标记它吗?
-
如果您使用特定技术标记问题,您应该为其提供上下文。如果唯一的问题是从现有数据库中读取外键,那么唯一涉及的技术可能是数据库系统。如果我理解错了,请添加涉及的源代码
标签: php laravel eloquent laravel-5.8