In my application, I have two tables, a Users table and a Forms table. The Users table is your typical user info with id, and the Forms table has a list of fields a user would need to complete. The many-many relationship is being tracked in my UserForms pivot table like so:
在我的应用程序中,我有两个表,一个Users表和一个Forms表。 Users表是具有id的典型用户信息,Forms表具有用户需要完成的字段列表。我的UserForms数据透视表中跟踪了很多关系,如下所示:
- id
- ID
- form_id (fk -> forms)
- form_id(fk - >表格)
- sender_id (fk -> users)
- sender_id(fk - > users)
- receiver_id (fk -> users)
- receiver_id(fk - >用户)
- completed
- 完成
A user may send another user a form to that he/she needs to complete, and a user can both send and receive many forms. A form may therefore have many users associated with it. The problem is when I bring up a form and try to eager load the relationship containing the User info for both the sender and receiver (since there are two foreign keys). It seems like I can only do one or the other. My form model looks like so:
用户可以向另一个用户发送他/她需要完成的表单,并且用户可以发送和接收许多表单。因此,表单可能有许多与之关联的用户。问题是当我调出一个表单并尝试加载包含发送者和接收者的用户信息的关系时(因为有两个外键)。好像我只能做一个或另一个。我的表单模型如下所示:
Form.php
form.php的
class Form extends Model
{
public function senders() {
return $this->belongsToMany('App\User', 'user_forms', 'form_id', 'sender_id')->withPivot('receiver_id', 'completed');
}
public function receivers() {
return $this->belongsToMany('App\User', 'user_forms', 'form_id', 'receiver_id')->withPivot('sender_id', 'completed');
}
}
FormController:
的FormController:
public function show( $id )
{
$form = Form::with('receivers')->findOrFail($id);
return view('form.show', ['form' => $form]);
}
view.blade.php
view.blade.php
@foreach( $form->receivers as $receiver )
<tr>
<td>{{ $receiver->full_name }} </td>
<td>{{ $receiver->pivot->sender_id }} </td>
</tr>
@endforeach
How can I get it to load the User attributed to sender_id, so I could do something like this in my view:
如何让它加载归属于sender_id的用户,所以我可以在我的视图中做这样的事情:
{{ $receiver->pivot->sender->full_name }}
Thanks in advance.
提前致谢。
1 个解决方案
#1
0
I don't think you can use relationships with implicit pivot tables.
我认为您不能使用隐式数据透视表的关系。
You could however create a FormUser model, setup the according hasMany and belongs to relations and then access the relations from there.
但是,您可以创建FormUser模型,设置相应的hasMany并属于关系,然后从那里访问关系。
Maybe I should explain that a bit more. Your FormUser would have two belongsTo relations (both pointing to the users table) and your view code would look like this:
也许我应该多解释一下。您的FormUser将有两个belongsTo关系(都指向users表),您的视图代码将如下所示:
@foreach( $form->formusers as $relation )
<tr>
<td>{{ $relation->receiver->full_name }} </td>
<td>{{ $relation->sender->full_name }} </td>
</tr>
@endforeach
#1
0
I don't think you can use relationships with implicit pivot tables.
我认为您不能使用隐式数据透视表的关系。
You could however create a FormUser model, setup the according hasMany and belongs to relations and then access the relations from there.
但是,您可以创建FormUser模型,设置相应的hasMany并属于关系,然后从那里访问关系。
Maybe I should explain that a bit more. Your FormUser would have two belongsTo relations (both pointing to the users table) and your view code would look like this:
也许我应该多解释一下。您的FormUser将有两个belongsTo关系(都指向users表),您的视图代码将如下所示:
@foreach( $form->formusers as $relation )
<tr>
<td>{{ $relation->receiver->full_name }} </td>
<td>{{ $relation->sender->full_name }} </td>
</tr>
@endforeach