@priyajey either (i) you have not defined a category relation on the Subcategories model, or (ii) one of the Subcategories instances returned from the query does not have an associated Category
Categories, Subcategories model has the following codes.
Categories model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
protected $guarded = [];
public $timestamps = false;
public function subcategories()
{
return $this->hasMany(Subcategories::class, 'category_id')->where('mode', 'on');
}
}
Subcategories model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Subcategories extends Model
{
protected $guarded = [];
public $timestamps = false;
public function category()
{
return $this->belongsTo(Categories::class);
}
}
@priyajey try to exclude any Subcategories instance(s) that does not have a parent Categories (your class naming is awful.) And really, the query should be handled outside the view template!
@foreach (App\Models\Subcategories::has('category')->with(['category:id,slug'])->where('mode', 'on')->get() as $subcategory)
@priyajey I notice that you are using plural names for the Model names. It is better to follow conventions and use singular names. For example, use 'Category' instead of 'Categories' for Model names.
What is the column name for the category in the table for Subcategories?