douhui4699 2017-08-14 01:53
浏览 6
已采纳

传递数据关系以查看laravel 5.2

I finish to make relation in my model, but i dont know to passing data relation to view, hope you can help me.

Model Siswa

 public function Absen()
{
    return $this->hasMany(Absen::class);
}

Model Absen

public function Siswa()
{
    $this->belongsTo(Siswa::class);
}

Absen tabel

Absen Table

Siswa Table

Siswa Table

AbsenController@index

public function index()
{

    $absen = Absen::where('level', '=', 'Siswa')->get();

    return view('absen.index')->with('data', $absen);
}

index.blade.php

@foreach($data as $index => $value)
            <tr>
              <td>{{ $index+1 }}</td>
              <td>{{ $value->nama }}</td>
              <td>{{ $value->keterangan }}</td>
              <td>
                {!! Form::open(['route' => ['siswa.destroy', $value->id], 
               'method' => 'DELETE']) !!}
                  {{ Form::submit('Hapus', ['class' => 'btn btn-danger']) }}
                  <a href="{{ route('siswa.edit', $value->id) }}" class="btn 
                btn-warning">Edit</a>
                {!! Form::close() !!}
              </td>
            </tr>
            @endforeach

The problem is the $value->nama not showing in my view, but i done to create relation in table siswa and table absen.Thank

  • 写回答

1条回答 默认 最新

  • ds342222 2017-08-14 02:11
    关注

    nama is not an attribute of Absen class, yet your value is an Absen class. Though you have defined the relationship between Absen and Siswa, attributes of Siswa are still not directly inherited to Absens. You can first get the Siswa of the Absen and then get the attributes of the Siswa.

    So what you have to do is:

    Call $value->Siswa->nama instead of $value->nama, and you should have your nama displayed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大