In this guide, we will walk you through the steps of displaying courses and categories in Laravel, a popular PHP framework. This guide is designed for beginners, so no prior experience with Laravel is required. By the end of this guide, you will have a good understanding of how to display courses and categories in a Laravel application.
Prerequisites
Before we begin, you should have the following tools installed:
- PHP 7.3.0 or higher
- Composer, a dependency manager for PHP
- A web server that supports PHP, such as Apache or Nginx
Additionally, you should have some familiarity with HTML, CSS, and JavaScript. If you are new to these technologies, there are many resources available online to help you get started.
Creating a New Laravel Application
To create a new Laravel application, open a terminal window and run the following command:
composer create-project --prefer-dist laravel/laravel course-catalog
This will create a new Laravel application in a directory called course-catalog.
Creating a Course Model
Next, we need to create a model for our courses. A model is a PHP class that represents a database table. In this guide, we will create a model called Course.
To create a new model, run the following command:
php artisan make:model Course
This will create a new file called Course.php in the app/Models directory. Open this file in a text editor and add the following code:
belongsTo(Category::class);
}
}
This code defines a Course model with the following properties:
$fillable: An array of fields that can be mass-assigned when creating or updating a course.category(): A relationship method that defines a one-to-many relationship between courses and categories.
Creating a Category Model
Next, we need to create a model for our categories. To create a new model, run the following command:
php artisan make:model Category
This will create a new file called Category.php in the app/Models directory. Open this file in a text editor and add the following code:
hasMany(Course::class);
}
}
This code defines a Category model with the following properties:
$fillable: An array of fields that can be mass-assigned when creating or updating a category.courses(): A relationship method that defines a one-to-many relationship between categories and courses.
Creating a Database Migration
Next, we need to create a database migration for our courses. A migration is a PHP class that defines the structure of a database table. In this guide, we will create a migration for the courses table.
To create a new migration, run the following command:
php artisan make:migration create_courses_table --create=courses
This will create a new file called xxxx_xx_xx_xxxxxx_create_courses_table.php in the database/migrations directory. Open this file in a text editor and add the following code:
id();
$table->string('name');
$table->text('description')->nullable();
$table->unsignedBigInteger('category_id');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('courses');
}
}
This code defines a migration that creates a courses table with the following columns:
id: An auto-incrementing primary key.name: A string field for the course name.description: A text field for the course description.category\_id: An unsigned big integer field for the course category ID.created\_atandupdated\_at: Timestamps for when the course was created and updated.
Additionally, this code defines a foreign key constraint for the category\_id column. This constraint ensures that the category ID referenced by a course exists in the categories table.
To create the courses table, run the following command:
php artisan migrate
This will run the migration and create the courses table in the database.
Creating a Course Controller
Next, we need to create a controller for our courses. A controller is a PHP class that handles HTTP requests and responses. In this guide, we will create a controller called CourseController.
To create a new controller, run the following command:
php artisan make:controller CourseController
This will create a new file called CourseController.php in the app/Http/Controllers directory. Open this file in a text editor and add the following code:
get();
return view('courses.index', compact('courses'));
}
}
This code defines a CourseController class with the following method:
index(): A method that retrieves all courses and their categories from the database and passes them to a view calledcourses.index.
Creating a Course View
Next, we need to create a view for our courses. A view is a PHP file that contains HTML and other markup. In this guide, we will create a view called courses.index.
To create a new view, create a new directory called courses in the resources/views directory. Then, create a new file called index.blade.php in the courses directory. Open this file in a text editor and add the following code:
@extends('layouts.app')
@section('content')
Courses
Name
Category
Description
@foreach($courses as $course)
{{ $course->name }}
{{ $course->category->name }}
{{ $course->description }}
@endforeach
@endsection
This code defines a courses.index view that extends a layout called layouts.app and displays a table of courses with their names, categories, and descriptions.
Creating a Route
Finally, we need to create a route for our courses. A route is a URL pattern that maps to a controller action. In this guide, we will create a route for the CourseController@index action.
To create a new route, open the routes/web.php file in a text editor and add the following code:
Route::get('/courses', [CourseController::class, 'index'])->name('courses.index');
This code defines a route for the /courses URL pattern that maps to the CourseController@index action.
Testing the Application
To test the application, start the Laravel development server by running the following command:
php artisan serve
Then, open a web browser and navigate to http://localhost:8000/courses. You should see a table of courses with their names, categories, and descriptions.
In this guide, we have shown you how to display courses and categories in Laravel. We have covered the following topics:
- Creating a new Laravel application
- Creating a course model
- Creating a category model
- Creating a database migration
- Creating a course controller
- Creating a course view
- Creating a route
- Testing the application
References
| Title | URL |
|---|---|
| Laravel Documentation | https://laravel.com/docs |
| Laravel Eloquent ORM Documentation | https://laravel.com/docs/eloquent |
| Laravel Routing Documentation | https://laravel.com/docs/routing |