'Attempt to read property "nama_jabatan" on int - Laravel 8

I have 4 models for my project.

Karyawan Role Jabatan Divisi

This is how I call the view:

https://codeshare.io/xvYz4B

and this is my model view on Karyawan:

<?php

namespace App\Models;

use App\Models\Cuti;
use App\Models\Role;
use App\Models\Divisi;
use App\Models\Jabatan;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Karyawan extends Model
{
    use HasFactory;

    protected $guarded = ['karyawan_id'];
    protected $table = 'karyawans';

    public function scopeSearch($query, array $searchs) {

        $query->when($searchs['search'] ?? false, function($query, $search) {
            return $query->where('nama', 'like', '%' . $search . '%')
            ->orWhere('divisi', 'like', '%' . $search . '%') 
            ->orWhere('jabatan', 'like', '%' . $search . '%')
            ->orWhere('agama', 'like', '%' . $search . '%')
            ->orWhere('nik', 'like', '%' . $search . '%'); 
        });
    }

    public function role_id()
    {
        return $this->belongsTo(Role::class);
    }

    public function divisi()
    {
        return $this->belongsTo(Divisi::class);
    }

    public function jabatan()
    {
        return $this->belongsTo(Jabatan::class);
    }

    public function cuti()
    {
        return $this->hasMany(Cuti::class);
    }

but it keeps getting error "Attempt to read property "nama_jabatan" on int". All I want is to display the name of "Jabatan" based on the ID.

Thank you before.



Solution 1:[1]

In your view change line $kar->jabatan_id->nama_jabatan To

$kar->jabatan->nama_jabatan

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nikistag