dongtuo3370 2019-04-12 08:15
浏览 144
已采纳

如何在laravel中链接多个外键

am working on a banking app, in my Laravel Model, i have a table called transactions.

In that table it has a column called to_id and from_id which is the id of the user that is sending money(from_id) and the id of who is receiving the money(to_id) which is linked to my User table,

here is the CreateTransactionsTable migration code

    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();
    });

here is the CreateUserTable migration file code

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();
        });

Here is the code for the Transactions Model

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

Here is the code for the Users Model

<?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');
    }
}

here's my controller code

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

here is my TranAdmin.blade.php code

<?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

My problem now is that i cant get the name of the person that sent $Tran->from_id and received $Tran->to_id the money

am getting this error

Trying to get property of non-object (View: C:\xampp\htdocs\laravel Projects\bank\iscbankesources\views\Admin\TranAdmin.blade.php) (View: C:\xampp\htdocs\laravel Projects\bank\iscbankesources\views\Admin\TranAdmin.blade.php)

i checked online but i saw was to do $Tran->User->name but since i got two columns that is linked to the users table, how can i do it

  • 写回答

1条回答 默认 最新

  • dongzong8110 2019-04-12 08:27
    关注

    you will need to define 2 more relationships on the Transaction model.

    like below

    as of now, you are trying to access properties of the relation from the attributes from the model (eg : when you do this$Trans->from_id, you are simply getting the data, not the relation.) rather you need to access the relation by defining them in the model first and then call the properties on it.

    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
    
    }
    

    and then in the template, you use it like follows

    $transaction->to->name;
    $transaction->from->name;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,