Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

satriamuda's avatar

Attempt to read property "name" on null

Model category

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Katagori extends Model
{
    use HasFactory;
    protected $fillable = ['name','slug'];

    public function post()
    {
        return $this->hasMany(Post::class);
    }
}

model post

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
    protected $fillable=[
        'seotitle',
        'keyword',
        'deskripsi',
        'title',
        'slug',
        'body',
      'user_id',
      'category_id',
        'thumbnail'
    ];

    public function tag()
    {
        return $this->belongsToMany(Tag::class);
    }

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

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getPictureAttribute()
    {
        return asset ('storage/'. $this->thumbnail);
    }
   

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable')->whereNull('parent_id');
    }
}

controller

<?php

namespace App\Http\Controllers;

use App\Models\Tag;


use App\Models\Menu;
use App\Models\Post;

use App\Models\Team;
use App\Models\Rating;
use App\Models\Partner;
use App\Models\Katagori;
use App\Models\Portfolio;
use App\Models\InsertHeader;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BerandaController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('welcome',[
            'post' => Post::paginate(2),
            'team' => Team::paginate(4),
            'portofolio' => Portfolio::get(),
            'partner' => Partner::get(),
            'rating' => Rating::get(),
            'info' => InsertHeader::paginate(1),
        ]);
    }

    public function about()
    {
        return view('home.about',[
            
           
           
        ]);
    }
    
      public function faq()
    {
        return view('home.faq',[
            
           
           
        ]);
    }
    
       public function lisensi()
    {
        return view('home.lisensi',[
            
           
           
        ]);
    }
    
    
    
    
    
    
    
    public function service()
    {
        return view('home.service',[
            
            'portofolio' => Portfolio::get(),
           
        ]);
    }




    public function porto()
    {
        return view('home.portofolio',[
            
            'portofolio' => Portfolio::all(),
           
        ]);
    }
    public function portof($slug)
    {
        
    	return view('home.portofoliodetail', [
           
    	    'portofolio' => Portfolio::where('slug', $slug)->get(),
        ]);
    }

    public function logo()
    {
       
        $logo = DB::table('Logos')->get();
        return view('layouts.app',[
            'logo' => $logo,
            
            ] );
    }

    

    public function blog()
    {
        return view('home.blog',[
            
            'post' =>  Post::latest()->paginate(6),
            'katagori' => Katagori::all(),
            'tag' => Tag::all(),
        ]);

    }
    
    
    // public function katagori(katagori $katagori){
    //     $katagori = Katagori::all();

    //     $data = $Katagori->post()->paginate(6);
    //     return view('home.blog', compact('data','katagori'));
    // }
    
    

    public function artikel($slug)
    {
        
    	return view('home.blogdetail', [
           
            'katagori' => katagori::all(),
            'tag' => Tag::all(),
    	    'post' => Post::where('slug', $slug)->get(),
        ]);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }



    public function website()
    {
        return view('home.layananwebsite',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }

    public function digital()
    {
        return view('home.digitalmarketing',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }

    public function seo()
    {
        return view('home.seo',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }

    public function maintenance()
    {
        return view('home.maintenance',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }
    public function program()
    {
        return view('home.program',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }

    public function ads()
    {
        return view('home.ads',[
            
            'portofolio' => Portfolio::paginate(6),
           
        ]);
    }
  
}
0 likes
7 replies
LaryAI's avatar
Level 58

The error "Attempt to read property 'name' on null" occurs when trying to access a property on a null object. In this case, it seems that the error is occurring in the Post model's katagori() method, where it is trying to access the name property of a katagori object that is null.

To fix this, you should check if the katagori object is null before trying to access its properties. You can do this by using the null coalescing operator (??) to provide a default value if the object is null. For example:

public function katagori()
{
    return $this->belongsTo(Katagori::class)->withDefault();
}

// Then, in your view, you can access the name property like this:
$post->katagori->name ?? 'No category'

This will return a new Katagori object with default values if the katagori object is null, so you can safely access its properties without getting an error.

vincent15000's avatar

You don't show the code where the problem occurs.

This errors mean that you are trying to read a property on a null object.

For example : imagine you are trying to get the name of a tag, you could have this code.

$tag->name;

But if $tag is null, you would try to get the name of a null object.

You need to find in your code where you're trying to access to a name property.

I would search ->name in the code.

Snapey's avatar

you cannot have a column called category_id and a relationship like

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

and expect laravel to know that katagori and category are the same thing

I cant actually believe you don't show us the full error message and the line that breaks !

2 likes
satriamuda's avatar

@Snapey

<x-layout>

@section('seo')
	<meta charset="utf-8" />
    <title> Artikel Terbaru tentang Desain dan Pengembangan Website | Pelajari Aspek Pentingnya</title>
    <meta
      name="description"
      content="Jelajahi daftar artikel jasa website terbaru yang memberikan informasi terkini tentang desain, pengembangan, dan optimisasi website. Pelajari strategi dan praktik terbaik dalam menciptakan online presence yang sukses."
    />
    <meta name="keywords" content="Jasa pembuatan website,Desain website,Pengembangan website,Optimisasi SEO,Responsif mobile,
    User experience (pengalaman pengguna),Tren desain web,Strategi pemasaran online,Teknologi web terbaru,Tips dan trik website" />

    <meta property="og:title" content="Artikel Terbaru tentang Desain dan Pengembangan Website | Pelajari Aspek Pentingnya" />
	<meta property="og:description" content="Jelajahi daftar artikel jasa website terbaru yang memberikan informasi terkini tentang desain, pengembangan, dan optimisasi website. Pelajari strategi dan praktik terbaik dalam menciptakan online presence yang sukses." />
	<meta property="og:image" content="https://www.perancangsitus.com/beranda/images/banner/6.jpg" />
	<meta property="og:url" content="https://www.perancangsitus.com/blog" />


    <meta name="author" content="Abdul Rozak" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="theme-color" content="#101010" />
@endsection

 <!-- Start Hero -->
 <section class="breadcrumb-area banner-2">
  <div class="text-block">
    <div class="container">
      <div class="row">
        <div class="col-lg-12 v-center">
          <div class="bread-inner">
            <div class="bread-menu">
              <ul>
                <li><a href="{{ route('beranda') }}">Home</a></li>
                <li><a href="{{ route('artikel') }}">Artikel</a></li>
              </ul>
            </div>
            <div class="bread-title">
              <h2>Artikel Terbaru</h2>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>
<!--End Breadcrumb Area-->
<!--Start Blog Grid-->
<section class="blog-page pad-tb pt40">
  <div class="container">

<div class="row">
  @foreach ($post as $item)
    
  
<div class="col-lg-4 mt60">
  <div class="single-blog-post- shdo">
    <div class="single-blog-img-">
      <a href="{{ route('artikeldetail', $item->slug) }}"><img src="{{ $item->picture }}" alt="girl" class="img-fluid"></a>
      <div class="entry-blog-post dg-bg2">
        <span class="bypost-"><a href="{{ route('artikeldetail', $item->slug) }}"><i class="fas fa-tag">{{ $item->katagori->name }}  </i> {{ $item->user->name }} </a></span>
        <span class="posted-on-">
          <a href="{{ route('artikeldetail', $item->slug) }}"><i class="fas fa-clock"></i> {{$item->created_at->diffForHumans() }}</a>
        </span>
      </div>
      
    </div>
    <div class="blog-content-tt">
      <div class="single-blog-info-">
        <h4><a href="{{ route('artikeldetail', $item->slug) }}">{{$item->title}}</a></h4>
        <p>{!! Str::limit($item->body, 100) !!}</p>
      </div>
      
      <div class="post-social">
        <div class="ss-inline-share-wrapper ss-hover-animation-fade ss-inline-total-counter-left ss-left-inline-content ss-small-icons ss-with-spacing ss-circle-icons ss-without-labels">
          <div class="ss-inline-share-content">
            <div class="ss-social-icons-container">
              <a href="javascript:void(0)">Shares</a>
              <a href="javascript:void(0)" target="blank"><i class="fab fa-facebook"></i></a>
              <a href="javascript:void(0)" target="blank"><i class="fab fa-twitter"></i></a>
              <a href="javascript:void(0)" target="blank"><i class="fab fa-linkedin"></i></a>
              <a href="javascript:void(0)" target="blank"><i class="fas fa-envelope"></i></a>
              <a href="javascript:void(0)" target="blank"><i class="fab fa-facebook-messenger"></i></a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
@endforeach

<nav aria-label="...">
  <ul class="pagination pagination-sm">
   {{ $post->links('pagination::simple-bootstrap-5') }}

  </ul>
</nav>


 
</div>
    
  </div>
</section>
<!--End Blog Grid--></x-layout>
1 like
Snapey's avatar

@satriamuda its what Lary says, item does not have kategori

but we're only guessing because you dont share the error

1 like
vincent15000's avatar

@satriamuda

Your problem is probably that $item (which is a ````$post) has no katagori```.

<span class="bypost-">
		<a href="{{ route('artikeldetail', $item->slug) }}">
				<i class="fas fa-tag">{{ $item->katagori->name }}</i>
				{{ $item->user->name }}
		</a>
</span>

Have you declared the relationship inside the Post model ?

Furthermore I suggest you to respect the Laravel naming convention to avoid complicated situations.

Please or to participate in this conversation.