0
votes

edit function

public function edit($id)
{
   $spesifikasi = logistik_spesifikasi::find($id);
   return view('logistik.edit_spesifikasi', get_defined_vars());
}

logistik_spesifikasi model

class logistik_spesifikasi extends Model
{
    protected $table = 'logistik_spesifikasi';
    protected $primarykey = 'id';
    protected $fillable = ['nama_produk', 'satuan', 'tipe', 'jumlah', 'id_satuan','id_produk'];

    public function listMaterial()
    {
        return $this->hasMany('App\ListMaterial', 'id_produk');
    }


    public function satuanList()
    {
        return $this->belongsTo('App\LogistikSatuan', 'id_satuan');
    }

}

ListMaterial Model:

class ListMaterial extends Model
{
    protected $table = 'list_material';

    protected $primarykey = 'id';
    public $timestamps = false;

    protected $fillable = ['id_material', 'id_produk', 'volume'];

    public function material()
    {
        return $this->hasMany('App\material', 'id_material');
    }

}

edit_spesifikasi.blade.php

<thead>
<tr>
<th>Nama Material</th>
</tr>
</thead>
<tbody>
@foreach($spesifikasi as $row )
    <tr class="gradeU">
     <td>{{ $row->listMaterial->first()->material->first()->nama_material }}</td>
    </tr>
@endforeach
</tbody>

gettting error

Trying to get property of non-object (View: C:\xampp\htdocs\hutama-prima\resources\views\logistik\edit_spesifikasi.blade.php)

1
Where is the code where you are passing $spesifikasi variable to view?Vishal
this public function edit($id) { $spesifikasi = logistik_spesifikasi::find($id); return view('logistik.edit_spesifikasi', get_defined_vars()); } sorry, my english is not good. i'm indonesian :)user8665248
I believe its' view('logistik.edit_spesifikasi', get_defined_vars()); I'm not sure why you are using get_defined_vars()?Vishal
like this ?? public function edit($id) { $spesifikasi = logistik_spesifikasi::find($id); return view('logistik.edit_spesifikasi', compact('spesifikasi')); }user8665248
You can send like this :return view('logistik.edit_spesifikasi', ['spesifikasi'=>$spesifikasi]);Vishal

1 Answers

0
votes

First check

$spesifikasi = logistik_spesifikasi::find($id);

$spesifikasi does not empty. If $spesifikasi is empty then you get this error.

Then,

public function edit($id)
{
   $spesifikasi = logistik_spesifikasi::find($id);
   if($spesifikasi)
   {
       return view('logistik.edit_spesifikasi', compact('spesifikasi'));
   }
  else
   { 
      //empty....
   }

}

Use this code.

    @foreach($spesifikasi->listMaterial as $row )
      <tr class="gradeU">
      <td>{{ $row->nama_material }}</td>
      </tr>
    @endforeach