Php 拉威尔:我如何只获取渴望加载的元素并将它们全部放入一个数组中?
我有两种模式:对话和事件,它们在许多关系中都是相关的。我想加载事件并将其全部放在一个数组中。我该怎么做Php 拉威尔:我如何只获取渴望加载的元素并将它们全部放入一个数组中?,php,laravel,laravel-4,eloquent,Php,Laravel,Laravel 4,Eloquent,我有两种模式:对话和事件,它们在许多关系中都是相关的。我想加载事件并将其全部放在一个数组中。我该怎么做 这些是我的模型和它们之间的关系 对话用户 public function events(){ return $this->belongsToMany('Events'); } $loginuser = Auth::user(); $convUsers = ConversationsUser::with('Events')->where('user_id'
这些是我的模型和它们之间的关系 对话用户
public function events(){
return $this->belongsToMany('Events');
}
$loginuser = Auth::user();
$convUsers = ConversationsUser::with('Events')->where('user_id','LIKE',$loginuser->id)
->has('events');
$events = $convUsers->get()->fetch('events')->toJson();
事件
public function conversations(){
return $this->belongsToMany('ConversationsUser');
}
控制器(我到目前为止所做的)
不想要的结果
[
[
{
"event_id":3
"conversaitons_id":1
}
],
[
{
"event_id":5,
"conversations_id":23
},
{
"event_id":6,
"conversations_id":23
}
]
]
[
{
"event_id":3
"conversations_id":1
},
{
"event_id":5,
"conversations_id":23
},
{
"event_id":6,
"conversations_id":23
}
]
首选结果
[
[
{
"event_id":3
"conversaitons_id":1
}
],
[
{
"event_id":5,
"conversations_id":23
},
{
"event_id":6,
"conversations_id":23
}
]
]
[
{
"event_id":3
"conversations_id":1
},
{
"event_id":5,
"conversations_id":23
},
{
"event_id":6,
"conversations_id":23
}
]
我建议用另一种方式,比如:
$conversation_ids = ConversationsUser::whereUserId($loginuser->id)->get(['conversations_id'])->toArray();
$events = Events::whereIn('conversations_id', $conversation_ids);
这是一条路。我也想过,如果我不能利用好这一千千万万的关系,我也会把这条路作为最后的选择。顺便说一句,我会做
ConversationsUser::whereUserId($logiuser->id)->列表('conversations\u id')
。顺便说一句,请用Events
替换Event
,因为它们是两个非常不同的类:一个是Laravel的默认类,另一个是定制类。