简体   繁体   中英

Trying to get property of non-object. Eloquent laravel 5.2

i have 3 tables with relation.

users, dosen, and statusdosen

i want to show data from status to dosen view. i already create function :

Dosen model :

 public function user()

{
    return $this->belongsTo('App\User');
}

public function dosen()

{
    return $this->belongsTo('App\Dosen');
}

public function status()
{
   return $this->belongsTo('App\StatusDosen');
} 

User model :

public function dosen()
{
return $this->hasOne('App\Dosen');
}

public function status()
{
return $this->hasOne('App\StatusDosen');
}

Status Dosen :

public function user()

{
    return $this->belongsTo('App\User');
}

public function dosen()

{
    return $this->belongsTo('App\Dosen');
}

    public function status()
{
return $this->belongsTo('App\StatusDosen');
}

and method in controller :

 public function status()
{
    $dosen = Dosen::paginate(10);
    return view('admin/dosen.status', compact('dosen'));
}

Status View :

@foreach($dosen as $key => $value)
                    <tr>
                        <td>{{ $value->namadosen }}</td>
                        <td>
                            @if ($value->status->status) 
                            <span class="label label-success">Sedang Dikampus</span>
                            @else
                            <span class="label label-danger">Tidak Dikampus</span>
                            @endif                                      
                        </td>
                        <td>
                         {!! Form::open(['url' => 'admin/dosen/status' . $value->user_id, 'style'=>'display:inline-block']) !!}
                            {!! Form::select('status', array('1' => 'Dikampus', '0' => 'Tidak Dikampus'), null, ['placeholder' => 'Pilih Status'], ['class' => 'form-control']) !!} 
                            <br><br>
                            {!! Form::button('<i class="fa fa-check-square-o"></i> Simpan', ['type' => 'submit', 'class' => 'btn btn-primary btn-sm'] )  !!}
                            {!! Form::close() !!}
                        </td>
                    </tr>
                    @endforeach

please correct my code.

Assuming you want your dosen data with statusdosen , you will have to change your status function like below:

public function status()
{
    $dosen = Dosen::with('status')->paginate(10);
    return view('admin/dosen.status', compact('dosen'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM