如何在拉拉维尔(Laravel)中对许多人建立反向关系
我有两个模型是用户和交易,我已经建立了一段关系,我的Hasmany关系正常工作,但是它的反向关系不起作用,这是我的交易迁移。
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->string('transaction_id');
$table->foreignId('transaction_from')
->constrained('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreignId('transaction_to')
->constrained('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->integer('transaction_coins');
$table->string('transaction_note');
$table->boolean('transaction_status');
$table->timestamps();
});
这是我的用户模型,我创建了两个关系。
public function transactions_from()
{
return $this->hasMany(Transaction::class, 'transaction_from');
}
public function transactions_to()
{
return $this->hasMany(Transaction::class, 'transaction_to');
}
这很好地工作了,我从交易和交易中获得了交易,但是当我创建反向关系功能时,我没有得到所需的结果。
public function user_from()
{
return $this->belongsTo(User::class, 'transaction_from', 'id');
}
public function user_to()
{
return $this->belongsTo(User::class, 'transaction_to', 'id');
}
在我的刀片文件中,我想显示从交易中进行交易并用交易显示的用户名称的名称。
<td>
<div class="d-flex align-items-center">
<img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
<span>{{ $transaction->transaction_from->user_from->name }}</span>
</div>
</td>
请给我建议我错了。
I have two models that are User and Transactions and I have made a relationship OneToMany, my hasMany Relationship is working fine, but its reverse relationship is not working here is my Transaction Migration.
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->string('transaction_id');
$table->foreignId('transaction_from')
->constrained('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->foreignId('transaction_to')
->constrained('users')
->onUpdate('cascade')
->onDelete('cascade');
$table->integer('transaction_coins');
$table->string('transaction_note');
$table->boolean('transaction_status');
$table->timestamps();
});
and Here is my User Model, I created two Relationships.
public function transactions_from()
{
return $this->hasMany(Transaction::class, 'transaction_from');
}
public function transactions_to()
{
return $this->hasMany(Transaction::class, 'transaction_to');
}
This is working fine I got Transactions From and Transactions To but when I created Reverse Relationship Functions I am not getting required results.
public function user_from()
{
return $this->belongsTo(User::class, 'transaction_from', 'id');
}
public function user_to()
{
return $this->belongsTo(User::class, 'transaction_to', 'id');
}
and in my blade file I want to show the Name of the User who Made Transaction From and To show the User Name with Transaction To.
<td>
<div class="d-flex align-items-center">
<img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
<span>{{ $transaction->transaction_from->user_from->name }}</span>
</div>
</td>
Please suggest me where I am wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经在交易模型上与用户模型上的user_to和user_th有关系。
而且,如果您的刀片$交易是您的交易模型对象,则应该这样做:
you have made relation on your Transaction model with user_to and user_from on User model.
and if here on your blade $transaction is your object of Transaction model you should do like this: