First time working with eloquent
relationship. I'm trying to access the subcategory method
yet I get this error:
Property [subcategory] does not exist on this collection instance.
New to laravel so any help would be appreciated!
blade
<table class="table">
<thead>
<tr>
<th scope="col">Category</th>
<th scope="col">Sub-category</th>
</tr>
</thead>
<tbody>
@foreach ($categories as $cat)
<tr>
<td scope="row">{{$cat->name}}</td>
//error here
@foreach ($categories->subcategory as $item)
<td>{{$item->name}}</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
category model
class Category extends Model
{
protected $fillable = ([
'name'
]);
public function subcategory(Type $var = null)
{
# code...
return $this->hasMany(Subcategory::class,'category_id','id');
}
}
subcategory model
class Subcategory extends Model { protected $fillable = ([ 'category_id', 'name' ]);
public function category(Type $var = null)
{
# code...
return $this->belongsTo(Category::class);
}
}
controller
public function create()
{
$categories = Category::all();
// dd($categories);
return view('settings.create', compact('categories'));
}