Setting Models
Create table :
- Category.php
- SubCategory.php
OR artisan
- php artisan make:model Category -ms
- php artisan make:model SubCategory -ms
Open file and add code
- Category.php
protected $table = 'category';
public function subcategory()
{
return $this->belongsTo(SubCategory ::class, 'id');
}
- SubCategory.php
protected $table = 'subcategory';
public function category()
{
return $this->hasMany(Category ::class, 'id_category');
}
Setting Controller
Create controller on terminal
- php artisan make:controller LandingpageController --resource
Open file LandingpageController.php and add code in function index()
use App\Models\Category;
$cat = Category::with('subcategory')->get();
return view('index', compact('cat'));
Setting Blade
Create or open file index.blade.php
@foreach($cat as $item)
{{item->name_category}}
@foreach(item->subcategory as $sub)
{{item->name_subcategory}}
@endforeach
@endforeach
Posting Komentar untuk "Join two table use laravel 8"