Fariza
Fariza

Reputation: 33

show all data from relational table laravel

I want to show all DataSiswa from transaksisetoran blade, here is the code

DataSiswa Model

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
    
class DataSiswa extends Model {
    public function transaksi_setoran() {
        return $this->hasMany('App\TransaksiSetoran');
    }
    
    protected $table = 'data_siswa';
    protected $fillable = ['nis','nama','jk','kelas','tahunajaran'];
}

TransaksiSetoran Model

<?php
namespace App;
    
use Illuminate\Database\Eloquent\Model;
    
class TransaksiSetoran extends Model {
    public function data_siswa() {
        return $this->belongsTo('App\DataSiswa','id');
    }
    
    protected $table = 'transaksi_setoran';
    protected $fillable = ['id_siswa','tanggalsetoran','nominal'];
}

Transaksisetoran.blade.php

.
.
.
    @foreach($transaksisetoran as $ang)
                 <tr>
                   <td>{{ $loop->iteration }}</td>
                   <td>{{ $ang->tanggalsetoran }}</td>
                   <td>
                    {{ $ang->data_siswa['nis'] }}
                   </td>
                   <td>
                     {{ $ang->data_siswa['nama'] }}
                   </td>
                   <td>
                     {{ $ang->data_siswa['kelas'] }}
                   </td>
                  <td>{{ $ang->nominal }}</td>
                  <td >
                     <a href="/admin/transaksisetoran/edit/{{ $ang->id }}" class="btn btn-warning" style="width:100%;">Edit</a>
                     <a href="/admin/transaksisetoran/hapus/{{ $ang->id }}" class="btn btn-danger" style="width:100%;">Hapus</a>
                   </td>
                 </tr>
                 @endforeach
.
.
.

Data is shown but its just 1st data on table data_siswa data on my web

and this is data on data_siswa table data on table data_siswa

what should I do to make data on table data_siswa show up?

Thank you!

Upvotes: 1

Views: 68

Answers (3)

Fariza
Fariza

Reputation: 33

i just change function on class datasiswa and transaksisetoran, also change on transaksisetoran controller for using compact()

Model DataSiswa

class DataSiswa extends Model
{
  public function transaksi_setoran()
  {
    return $this->hasMany(TransaksiSetoran::class);
  }

  protected $table = 'data_siswa';
  protected $fillable = ['nis','nama','jk','kelas','tahunajaran'];
}

Model TransaksiSetoran

class TransaksiSetoran extends Model
{
  protected $table = 'transaksi_setoran';
      protected $fillable = ['id_siswa','tanggalsetoran','nominal'];
      public function data_siswa()
      {
        return $this->belongsTo(DataSiswa::class,'id_siswa');
      }
}

on transaksisetoran controller

public function index()
    {
        $transaksisetoran = TransaksiSetoran::paginate(5);
        return view('/admin/transaksisetoran/transaksisetoran')
        ->with(compact('transaksisetoran'));
    }

Upvotes: 0

user13091150
user13091150

Reputation:

$DataSiswa = DataSiswa::get();

foreach ($DataSiswa as $OneDataSiswa) {
   foreach ($OneDataSiswa->transaksi_setoran() as $item) {
      echo $item->id;
  }
}

Upvotes: 0

Atif Mahmood
Atif Mahmood

Reputation: 390

i hope it will work for you

$get = DB::table('data_siswa')
               ->join('transaksi_setoran', 'transaksi_setoran.id_siswa', '=', 'data_siswa.id')
           ->select(data_siswa.*)
           ->get();
return $get;

Upvotes: 1

Related Questions