簡體   English   中英

Laravel 4.2中雄辯關系的問題

[英]Issue with Eloquent Relationships in Laravel 4.2

我在Laravel 4.2中使用雄辯的ORM時遇到問題

我有2張桌子,銀行和bank_accounts

在銀行中,我有銀行的ID,在bank_accounts表中,我有bank_id,它是指向銀行表的外鍵。

我已經為此苦苦掙扎了好幾個小時,並在運氣不錯的情況下查看了Laravel文檔。

我有兩個模型:

銀行帳戶模型:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

    class Cuentas extends Eloquent 
    {
      protected $table      = 'icar_cuentas';
      protected $primaryKey = 'id';  
      public    $timestamps = FALSE;

      public function Banco()
      {
        return $this->belongsTo('Bancos','id_banco');
      }
    }

銀行型號:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class Bancos extends Eloquent 
{
  protected $table      = 'icar_bancos';
  protected $primaryKey = 'id';  
  public    $timestamps = FALSE;

  public function cuentasRelacionadas()
  {
    return $this->hasMany('Cuentas');
  }
}

我需要列出用戶添加的帳戶並顯示其相關銀行的名稱,但沒有弄清楚該怎么做。

到目前為止,我在控制器中:

public function showCuentas()
  {
    $results = Cuentas::all();

    return View::make('content.bancos.list_bancos', array('bancos' => $results));
  }

和視圖:

<tbody>
            @foreach($bancos as $banco)
                          @if($banco->activo == 'si')
                             {{-- */$estado = '<span class="label label-success">Activo</span>';/* --}}
                          @endif
                          @if($banco->activo == 'no')
                             {{-- */$estado = '<span class="label label-danger">Inactivo</span>';/* --}}
                          @endif
              <tr>
                <td><a href="{{ URL::to('cuentas/editar/'.Encryption::encode($banco->id)) }}"><i class="fa fa-edit"></i></a> | <a href="{{ URL::to('cuentas/eliminar'.Encryption::encode($banco->id)) }}" onclick="return confirma('Realmente deseas eliminar la cuenta {{ $banco->cuenta }}');"><i class="fa fa-trash-o"></i></a></td>
                <td>{{ $banco->banco->first()->banco }}</td>
                <td>{{ $banco->cuenta }}</td>
                <td>{{ $banco->tipo }}</td>
                <td>{{ $estado }}</td>
              </tr>
              @endforeach
            </tbody>

但它總是在表中顯示第一銀行的名稱,而不是相關的名稱。

感謝任何幫助

您不應該使用延遲加載,因為對於每個Cuenta ,您的數據庫中都會有另一個查詢。 使用預加載:

$results = Cuentas::with('banco')->get();

然后你去:

foreach($bancos as $banco) { //--> I think you should replace with cuentas, but it's up to you
    ....
    {{ $banco->banco->banco }} // also, terrible. Use banco.name instead. Give proper name to easily understand your code
    ....
}

好吧,醫生說方法Model :: belongsTo(laravel 4.2)

belongsTo( string $related, string $foreignKey = null, string $otherKey = null, string $relation = null)

我看到您傳遞給函數Cuentas :: belongsTo()的第二個參數是'id_cuentos',但是在Bancos類中,主鍵屬性說主鍵的名稱是'id'。

protected $primaryKey = 'id';

這不是有什么問題嗎?

public function Banco()belongsTo關系,將返回Bancos對象-而不是它們的集合。 因此,當您在該對象上調用->fist()時,它不是Collection::fist()函數,而是Bancos::first() 這就是為什么您始終從Bancos模型中獲得第一個條目的原因。

在您的foreach循環中, $banco是一個Cuentas對象。 $banco->banco是您需要的相關Bancos對象。 因此,如果您的Bancos對象中具有屬性$banco Bancos您需要在模板中調用的只是

{{ $banco->banco->banco }}

正如其他人在回答中提到的那樣,您應該將Banco()重命名為banco()或調用{{ $banco->Banco->banco }}

附帶說明:您的命名風格非常混亂。 為什么將Cuentas收藏品稱為$bancos

我將重命名一些東西:

  • function Banco() => function banco()
  • array('bancos' => $results) => array('cuentas' => $results)
  • @foreach($bancos as $banco) => @foreach($cuentas as $cuenta)
  • {{ $banco->banco->banco }} => {{ $cuenta->banco->banco }}

另外(如果可能),我會將屬性Bancos::banco重命名為Bancos::name以便您可以調用{{ $cuenta->banco->name }}而不會引起審閱者的困惑。

我非常確定事情是區分大小寫的,並且您在模型中使用Banco,在視圖中使用Banco。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM