Rayan Suryadikara
Rayan Suryadikara

Reputation: 247

Relationship hasone trying to get property of non-object

I got a problem with relationship one to one mechanism for edit & update condition, so I have model Siswa and Telepon, with Telepon belongs to Siswa... here is the code

Siswa.php (model)

class Siswa extends Model
{
protected $table = 'siswa';

protected $fillable = [
    'nisn',
    'nama_siswa',
    'tgl_lahir',
    'jns_klmin'
];

protected $dates = ['tgl_lahir'];

public function getNamaSiswaAttribute($nama_siswa){
    return ucwords($nama_siswa);
}

public function setNamaSiswaAttribute($nama_siswa){
    $this->attributes['nama_siswa']=ucwords($nama_siswa);
}

public function telepon(){
    return $this->hasOne('App\Telepon', 'id_siswa');
}
}

Telepon.php (model)

class Telepon extends Model
{
protected $table = 'telepon';

protected $primKey = 'id_siswa';

protected $fillable = [
    'id_siswa',
    'no_telepon',
];

public function siswa(){
    return $this->belongsTo('App\Siswa', 'id_siswa');
}
}

Edit and update function controller shown as follows :

public function edit($id){
    $siswa = Siswa::findOrFail($id);
    $siswa->no_telepon = $siswa->telepon->no_telepon;
    return view('siswa.edit', compact('siswa'));
}

public function update(Request $request, $id){
    $siswa = Siswa::findOrFail($id);
    $input = $request->all();

    $validator = Validator::make($input, [
        'nisn'=>'required|string|size:4|unique:siswa,nisn,'.$request->input('id'),
        'nama_siswa'=>'required|string|max:30', 
        'tgl_lahir'=>'required|date',
        'jns_klmin'=>'required|in:L,P',
        'no_telepon'=>'sometimes|numeric|digits_between:10,15|unique:telepon,no_telepon,'.$request->input('id').',id_siswa',
    ]);

    if ($validator->fails()) {
        return redirect('siswa/'.$id.'/edit')->withInput()->withErrors($validator);
    }

    $siswa->update($request->all());

    $telepon = $siswa->telepon;
    $telepon->no_telepon = $request->input('no_telepon');
    $siswa->telepon()->save($telepon);

    return redirect('siswa');
}

I got Trying to get property of non-object error in edit function, line "$siswa->no_telepon = $siswa->telepon->no_telepon;".

When we call edit view inside edit controller, it will give a form which inside of it has previous saved data. no_telepon itself is a column from Telepon table, not Siswa, so how to show telephone saved data for editing purposes is the problem. FYI, create works just fine, and no_telepon data saved in Telepon table. So, how about this one? Any help appreciated.

Upvotes: 0

Views: 662

Answers (1)

Filip Koblański
Filip Koblański

Reputation: 10018

It's probably because you don't have any 'App\Telepon' in the database with 'id_siswa' pointing to the id of the record from table siswa.

You can ommit this error in this way:

public function edit($id){
    $siswa = Siswa::findOrFail($id);
    $siswa->no_telepon = $siswa->telepon ? $siswa->telepon->no_telepon : '';
    return view('siswa.edit', compact('siswa'));
}

Upvotes: 0

Related Questions