从表中的hasMany循环数据时,我得到一个错误“试图获取财产上的非对象‘perbaikan’”

问题描述 投票:1回答:1

我得到一个错误,试图让房地产非对象的“perbaikan”。请帮我解决我的问题。

My Perbaikan model
public function tagihan()
    {
        return $this->belongsTo('App\Tagihan', 'id_tagihan');
    }

My Tagihan model
  public function perbaikan()
    {
        return $this->hasMany('App\Perbaikan');
    }

My controller
public function report(Tagihan $tagihan)
{
   return view('tagihan.report', compact('tagihan'));
}

My view blade
<tbody>
   <?php $no = 0;?>
   @foreach ($tagihan as $data)
   @foreach ($data->perbaikan as $row)
   <?php $no++ ;?>
     <tr>
         <td>{{ $no }}</td>
         <td>{{ $row -> nomor_dokumen }}</td>
         <td>{{ $row -> kulkas -> nomor_asset  }}</td>
         <td>{{ $row -> tipepekerjaan -> kode_tipe_pekerjaan }}</td>
         <td>{{ $row -> tanggal_perbaikan }}</td>
     </tr>
   @endforeach
   @endforeach
 </tbody>

DD($法案)

Tagihan {#337 ▼
  #primaryKey: "id_tagihan"
  #guarded: []
  #connection: "mysql"
  #table: "tagihans"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [▼
    "id_tagihan" => 1
    "nomor_dokumen" => "A00001919191"
    "periode_tagihan" => "Januari"
    "created_at" => "2019-02-05 08:26:00"
    "updated_at" => "2019-02-05 08:26:00"
  ]
  #original: array:5 [▼
    "id_tagihan" => 1
    "nomor_dokumen" => "A00001919191"
    "periode_tagihan" => "Januari"
    "created_at" => "2019-02-05 08:26:00"
    "updated_at" => "2019-02-05 08:26:00"
  ]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
}

My perbaikans table
public function up()
    {
        Schema::create('perbaikans', function (Blueprint $table) {
            $table->increments('id_perbaikan');
            $table->string('nomor_dokumen_perbaikan', 25)->unique();
            $table->integer('id_tagihan')->unsigned();
            $table->string('teknisi', 30);
            $table->date('tanggal_perbaikan');
            $table->timestamps();
            $table->foreign('id_tagihan)->references('id_tagihan')->on('tagihans');
        });
    }

My tagihans table
 public function up()
    {
        Schema::create('tagihans', function (Blueprint $table) {
            $table->increments('id_tagihan');
            $table->string('nomor_dokumen', 25);
            $table->string('periode_tagihan', 20);
            $table->timestamps();
        });
    }

我试图从表中的hasMany得到的所有数据,它工作正常,但我不想从表中的所有数据。我只希望基于来自属于关联表中的ID检索数据

我得到消息,“试图获得财产非对象的“perbaikan”错误

laravel-5 relationship one-to-many
1个回答
0
投票

您是通过$tagihan循环,但是这是没有必要的

更改:

   @foreach ($tagihan as $data)
   @foreach ($data->perbaikan as $row)

至:

   @foreach ($tagihan->perbaikan as $row)

并取出@endforeach之一

© www.soinside.com 2019 - 2024. All rights reserved.