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.