问题描述
我第一次在 LaraVEL 5.1 中使用多态多对多关系但不知何故它不起作用我附上我的代码你能找出我做错了什么吗?
表格
用户表
id | name | email
-------------------------
1 | john | john@gmail.com
--------------------------
2 | adam | adam@gmail.com
公司表
id | name | email
-----------------
1 | tesla | tesla@gmail.com
calendar_subscriptions 表
id | user_id | resourceable_type | resourceable_id
--------------------------------------------------
1 | 1 | App\Models\User | 2
--------------------------------------------------
2 | 1 | App\Models\Company | 1
日历订阅模式
class CalendarSubscription extends Model
{
protected $table = 'calendar_subscriptions';
public function resourceable()
{
return $this->morphTo();
}
}
用户模型
class User extends Model {
public function calendarSubscription()
{
return $this->morphMany('App\Models\CalendarSubscription','resourceable');
}
}
尝试过
User::with('calendarSubscription')->where('id','=',Auth::user()->id)->get();
App\Models\User » (sql: select * from "calendar_subscriptions" where "calendar_subscriptions"."id" in (25469) and "calendar_subscriptions"."resourceable_id" = App\Models\User)
想要在收集表格中收集以下数据
id | name | email
-------------------------
1 | john | john@gmail.com
--------------------------
2 | adam | adam@gmail.com
--------------------------
1 | tesla | tesla@gmail.com
首先从 calendar_subscriptions 中获取 user_id = 1 的所有详细信息,然后获取 公司名和用户名。
我知道项目结构是错误的。我不能改变任何东西,必须遵循这个结构
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)