从hasMany表中循环数据时,出现错误“试图获取非对象的属性'perbaikan'”

时间:2019-02-06 08:01:57

标签: laravel-5 relationship one-to-many

我遇到了错误,试图获取非对象的属性“ 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)

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'”时出现错误

1 个答案:

答案 0 :(得分:0)

您正在通过 @foreach ($tagihan as $data) @foreach ($data->perbaikan as $row) 槽,但这不是必需的

更改:

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

收件人:

@endforeach

并删除Rename-Item $sdataJRN -NewName ($NumOne + $NumTwo + "99" + ".jrn") Rename-Item $sdataJNM -NewName ($NumOne + $NumTwo + "99" + ".jnm") Rename-Item $sdataTXN -NewName ($NumOne + $NumTwo + "99" + ".txn")

之一