如何在laravel中链接多个外键

时间:2019-04-12 08:15:01

标签: php laravel laravel-5 eloquent

在开发银行应用程序时,在我的Laravel模型中,我有一个称为交易记录的表。

在该表中,有一个名为to_id和from_id的列,该列是发送资金的用户的ID(from_id)和接收钱的用户的ID(to_id),该ID链接到我的User表

这是CreateTransactionsTable迁移代码

    Schema::create('transactions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('from_id')->unsigned();
        $table->foreign('from_id')->references('id')->on('users');
        $table->bigInteger('to_id')->unsigned();
        $table->foreign('to_id')->references('id')->on('users');
        $table->integer('Amount');
        $table->enum('TransactionType', ['Credit', 'Debit']);
        $table->enum('Status', ['Completed', 'Incomplete']);
        $table->timestamps();
    });

这是CreateUserTable迁移文件的代码

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');            
            $table->string('AccountNumber')->unique();
            $table->rememberToken();
        });

这是交易模型的代码

class Transactions extends Model{
    public function users(){
        return $this->belongsTo('App\Users');
    }
}

这是用户模型的代码

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password', 'Amount',
    ];

    public function transactions(){
        return $this->hasMany('App\Transactions');
    }
}

这是我的控制器代码

public function Transactions(){
        $Trans = Transactions::all();
        return view('Admin.TranAdmin')->with(['title'=>'Transaction History ', 'Trans'=>$Trans]);
    }

这是我的TranAdmin.blade.php代码

<?php $no = 1; ?>
    @foreach ($Trans as $Tran)  
        <tr>
             <td>{{ $no++ }}</td>
             <td>{{ $Tran->to_id }}</td>
             <td>{{ $Tran->from_id->name }}</td>
             <td>{{ $Tran->status }}</td>
             <td>{{ $Tran->created_at->format('F jS, Y')}}</td>
             <td></td>
         </tr>
     @endforeach

我现在的问题是我无法获得发送$Tran->from_id并收到$Tran->to_id钱的人的名字

出现此错误

  

试图获取非对象的属性(视图:C:\ xampp \ htdocs \ laravel Projects \ bank \ iscbank \ resources \ views \ Admin \ TranAdmin.blade.php)(视图:C:\ xampp \ htdocs \ laravel Projects \ bank \ iscbank \ resources \ views \ Admin \ TranAdmin.blade.php)

我在线检查了一下,但发现要进行$Tran->User->name,但是由于我有两个链接到users表的列,我该怎么做

1 个答案:

答案 0 :(得分:6)

您将需要在事务模型上再定义2个关系。

如下所示

到目前为止,您正在尝试从模型的属性访问关系的属性(例如:执行此操作 const token= "The registration token here"; const payload = { notification: { title: "Testing", body: "Test" } }; admin.messaging().sendToDevice(token, payload).then(response => { console.log( response.successCount + " messages were sent successfully" ); }); 时,您只是在获取数据,而不是在关系上。)但是,您需要通过首先在模型中定义它们然后访问其上的属性来访问该关系。

$Trans->from_id

然后在模板中,按如下所示使用它

class Transactions extends Model{
    public function from(){
        return $this->belongsTo('App\Users','from_id', 'id');
    }

    public function to(){
        return $this->belongsTo('App\Users', 'to_id', 'id');
    }

    //maybe you dont need the user relationship at all

}