Go to your App/Categories.php model and write
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
/**
* Get the comments for the blog post.
*/
public function subcategories()
{
return $this->hasMany('App\SubCategories','foreign_key_column_name');
}
}
Also in App/SubCategories.php model
public function categories()
{
return $this->belongsTo('App\Categories','id');
}
Then in your controller
$category = Category::find($id);
$subcategories= Category::find($id)->subcategories;
return view('index')->withCategory($category)->withSubcategories($subcategories);
And In your view
@foreach($subcategories as subcategory)
<span>{{ $subcategory->name}}</span>
@endforeach
For more details read https://laravel.com/docs/5.5/eloquent-relationships#one-to-many