代码之家  ›  专栏  ›  技术社区  ›  Douglas Roos

Laravel 4.2中的口才关系问题

  •  0
  • Douglas Roos  · 技术社区  · 8 年前

    我在Laravel 4.2中使用Eloquent ORM时遇到了一个问题

    我有两张桌子,银行和银行账户

    在banks中我有银行的ID,在bank_accounts表中我有bank_ID,它是指向banks表的外键。

    我已经为此苦苦挣扎了几个小时,并在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>
    

    但它总是在表中显示第一银行的名称,而不是相关银行的名称。

    感谢任何帮助

    4 回复  |  直到 8 年前
        1
  •  2
  •   Felippe Duarte    8 年前

    您不应该使用惰性加载,因为对于每个 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
        ....
    }
    
        2
  •  0
  •   erwan    8 年前

    嗯,医生说方法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';
    

    这不是有什么问题吗?

        3
  •  0
  •   Paul Spiegel    8 年前

    这个 public function Banco() 是一个 belongsTo 关系并将返回 Bancos 对象,而不是它们的集合。所以当你打电话的时候 ->fist() 在那个物体上它不是 Collection::fist() 功能-它是 Bancos::first() 这就是为什么你总是从Bancos模型中获得第一个条目。

    在foreach循环中 $banco 是Cuentas对象。和 $banco->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 }} 不会让评审员感到困惑。

        4
  •  -1
  •   bertmaclin    8 年前

    我很确定事情是区分大小写的,你在模型中使用Banco,在视图中使用Banco。