Database Migration & Model View Controller
Model:
- A Model is a file that describes 1 database table.
- It should be a singular form of the database table.
- It extends the
Model
class means it ties to the Database and ready to use.
class Category extends Model
{
use HasFactory;
}
Controller:
- It is a bridge that gets data from the database and passed it into the views.
- It also takes data from the view (shape of HTML Forms) and stored in the data.
class HomeController extends Controller
{
public function index()
{
// This will get all the records from the categories table
// and every record is an object and passed into the view
$allCategories = Category::all();
return view('index', compact('allCategories'));
}
}
View:
- Represents presentation layer of our application.
<div class="list-group">
@foreach($allCategories as $category)
<a href="/?category_id={{ $category->id }}" class="list-group-item">{{ $category->name }}</a>
@endforeach
</div>