Attempt to read property "name" on null
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Displaying Category Name in Your Laravel Application
Category Model Code:
class Category extends Model
{
use HasFactory;
protected $fillable= ['name','number'];
public function news(){
return $this->hasMany(News::class);
}
}
News Model Code:
class News extends Model
{
use HasFactory;
protected $fillable= ['cat_id','title','photo','description','author_id','status'];
public function category(){
return $this->belongsTo(Category::class);
}
public function author(){
return $this->belongsTo(Author::class);
}
}
Author Model Code:
class Author extends Model
{
use HasFactory;
protected $fillable= ['name','status'];
public function news(){
return $this->hasMany(News::class);
}
}
Controller Function:
public function news_index(){
$news= News::with('category')->get();
return view('News.list',compact('news'));
}
Blade Page Code:
@foreach($news as $new)
<tr>
<th scope="row">{{$loop->iteration}}</th>
<td> {{$new->category->name}}</td>
<td> {{$new->title}}</td>
<td> @if($new->photo)
<a href="{{url('storage/images/'.$new->photo)}}" target="_blank" class="btn btn-sm btn-secondary">Görüntüle</a></td>
@endif
<td> {{$new->author_id}}</td>
<td> {{$new->description}}</td>
<td> {{$new->status}}</td>
<td>
<a class="btn btn-danger" onclick="return confirm('Silmek istediğinize emin
misiniz?')" href="{{route('news_delete', $new->id)}}"><i class="fa fa-trash"></i></a>
<a class="btn btn-primary" href="{{route('news_update',$new->id)}}"><i class="fa
fa-pen"></i></a>
</td>
</tr>
@endforeach
Migration Table Code:
public function up()
{
Schema::create('news', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('cat_id');
$table->string('title');
$table->string('photo');
$table->longText('description');
$table->unsignedBigInteger('author_id');
$table->boolean('status')->default('1');
$table->foreign('cat_id')->references('id')->on('categories')->onDelete('cascade');
$table->foreign('author_id')->references('id')->on('authors')->onDelete('cascade');
$table->timestamps();
});
}
The error "Attempt to read property "name" on null" appears when you try to access the non-existent category name for a news item that isn't associated with any category. To solve this issue, ensure that each news article is connected to a proper category by using the correct relationships in your models.
First, update your Controller function as follows:
public function news_index(){
$news= News::with('category')->get();
if($news){
return view('News.list',compact('news'));
}else{
return redirect()->route('error')->withErrors("There is an error in fetching news or categories.")->withInput();
}
}
This code checks whether there are any news items and if so, it renders the view containing your loop with the necessary category information. Otherwise, it redirects to a custom error page, displaying an appropriate message. This ensures that your application will handle the situation where news or categories couldn't be fetched successfully.
Now, let's examine the relationships between the models. The 'category' belongsTo(News) relationship in the Category model must be changed as shown below to maintain consistency:
class Category extends Model
{
use HasFactory;
protected $fillable= ['name','number'];
public function news(){
return $this->hasMany(News::class);
}
}
Next, you need to modify the relationships in the News and Author models. Since each news item belongs to a category and author respectively, their relationships should be updated as follows:
class News extends Model
{
use HasFactory;
protected $fillable= ['cat_id','title','photo','description','author_id','status'];
public function category(){
return $this->belongsTo(Category::class);
}
public function author(){
return $this->belongsTo(Author::class);
}
}
class Author extends Model
{
use HasFactory;
protected $fillable= ['name','status'];
public function news(){
return $this->hasMany(News::class);
}
Now, the relationships between your models are consistent and clear. As a result, it will be much easier for you to implement them in your application without receiving any errors related to null properties or missing relationships.