Laravel 5.6嵌套口才关系

时间:2018-08-29 07:18:40

标签: laravel eloquent relationship

早上好,我目前正在研究与游戏相关项目的模型关系,我有一个与交易模型具有hasMany关系的银行模型

Schema::create('banks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name',30);
        $table->string('faction',10);
        $table->string('region',3);
        $table->integer('balance')->unsigned()->nullable();
        $table->timestamps();
        $table->softDeletes();
    });

模型是:

class Bank extends Model
{
   use SoftDeletes;

   /**
   * Establishes a oneToMany relationship with the Transaction
   * model
   *
   * @param
   * @return
   */
   public function transactions()
   {
       return $this->hasMany('App\Transaction');
   }
}

交易架构为:

Schema::create('transactions', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('bank_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->integer('operator_id')->unsigned();
        $table->string('operation', 128);
        $table->integer('amount');
        $table->string('note',255)->nullable();
        $table->timestamps();
        $table->softDeletes();

        $table->foreign('bank_id')
              ->references('id')
              ->on('banks')
              ->onUpdate('cascade');

        $table->foreign('user_id')
              ->references('id')
              ->on('users')
              ->onUpdate('cascade');

        $table->foreign('operator_id')
              ->references('id')
              ->on('users')
              ->onUpdate('cascade');

    });

用户架构为:

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

交易模型为:

class Transaction extends Model
{
use SoftDeletes;

public function banks()
{
    return $this->belongsTo('App\Bank');
}

public function users()
{
    return $this->belongsTo('App\User');
}
}

我想在Laravel中实现以下SQL:

SELECT t.amount, t.created_at, b.name, b.region, b.faction, u.name 
FROM transactions as t
JOIN users as u
JOIN banks as b ON t.bank_id = b.id
WHERE b.id =1
ORDER by t.created_at DESC

我设法编写了此代码,该代码返回了所选银行的所有交易,在这种情况下为1(但最终通过参数传递了该数据)

public function show($bank)
{
    $transactions = Bank::find($bank)->transactions()
                                     ->orderBy('created_at','DESC')
                                     ->get();
    dd($transactions);
}

但是我不知道如何还检索其余的SQL。 如有任何解释,我们将不胜感激。

上传此DD的Json的屏幕截图: Json

如何在属性中检索这两个user_id和operator_id的字段user.name?用Eloquent,而不是qBuilder

4 个答案:

答案 0 :(得分:0)

将该方法放入Transaction模型中,应该可以使用

 public function getByBankId($id)
    {
        return $this
            ->join("banks as b", "b.id", "=", "transactions.bank_id")
            ->join("users as u", "u.id", "=", "transactions.user_id")
            ->where("b.id", $id)
            ->orderBy("transactions.created_at", "DESC")
            ->selectRaw("transactions.amount, t.created_at, b.name, b.region, b.faction, u.name")
            ->get();
    }

答案 1 :(得分:0)

我建议您使用BelongsToMany关系:

class Bank extends Model {
    public function users() {
        return $this->belongsToMany(User::class, 'transactions')
            ->withPivot('amount')
            ->orderByDesc('transactions.created_at');
    }
}

foreach($bank->users as $user) {
    // $user->name
    // $user->pivot->amount
}

答案 2 :(得分:0)

您可以执行以下操作:

$transactions = \App\Transaction::with('bank', 'user')
    ->where('bank_id', $bank)
    ->orderBy('created_at', 'desc')
    ->get();

with()方法将关系加载到模型上。然后,您可以将这些关系视为属性,以从其中访问信息,例如

foreach ($transactions as $transaction) {

    $transaction->amount;

    $transaction->user->name;

    $transaction->bank->name;
    $transaction->bank->region;

}

有关更多信息,请查看documentation

答案 3 :(得分:0)

我用以下代码修复了代码:

$transactions = Transaction::with(['user', 'bank'])
                                ->where('bank_id',1)
                                ->limit(10)
                                ->get();
    //dd($transactions);

我还将模型交易关系修改为:

class Transaction extends Model
{
    use SoftDeletes;

    public function bank()
       {
          return $this->belongsTo('App\Bank');
       }

    public function user()
       {
         return $this->belongsTo('App\User');
       }
}

由于它们仅返回一种模型。