CRUD Library Book


Laravel CRUD Learning Project Documentation

Table of Contents

  1. Project Overview
  2. Features
  3. Technology Stack
  4. System Requirements
  5. Installation
  6. Project Structure
  7. Configuration
  8. Database Setup
  9. Running the Application
  10. API Endpoints
  11. CRUD Operations
  12. Troubleshooting

Project Overview

Belajar-Crud-Laravel is a learning project designed to demonstrate fundamental CRUD (Create, Read, Update, Delete) operations in Laravel. This project serves as an educational resource for developers who want to understand how to build basic web applications using the Laravel framework with proper database management practices.

The project follows Laravel’s best practices and conventions, making it an excellent reference for beginners and intermediate developers.


Features

  • Create Operations: Add new records to the database with validation
  • Read Operations: Retrieve and display data from the database
  • Update Operations: Modify existing records
  • Delete Operations: Remove records from the database
  • Database Migrations: Organized database schema management
  • Eloquent ORM: Object-oriented database interaction
  • Blade Templating: Dynamic view rendering
  • RESTful Routing: Standard HTTP method-based routing
  • Form Validation: Input validation and error handling

Technology Stack

  • Framework: Laravel (latest version)
  • Language: PHP
  • Database: MySQL/SQLite (configurable)
  • Frontend: Blade Templates with HTML/CSS
  • Build Tool: Vite
  • Package Manager: Composer
  • Testing: PHPUnit (pre-configured)

System Requirements

  • PHP >= 8.1
  • Composer
  • Node.js >= 16
  • MySQL or SQLite database
  • Apache/Nginx web server (or use Laravel’s built-in server)

Installation

Step 1: Clone the Repository

git clone https://github.com/BagasHtml/Belajar-Crud-Laravel.git
cd Belajar-Crud-Laravel

Step 2: Install PHP Dependencies

composer install

Step 3: Install Node Dependencies

npm install

Step 4: Create Environment File

cp .env.example .env

Step 5: Generate Application Key

php artisan key:generate

Step 6: Configure Database

Edit the .env file with your database credentials:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_crud
DB_USERNAME=root
DB_PASSWORD=

Step 7: Run Migrations

php artisan migrate

Project Structure

Belajar-Crud-Laravel/
├── app/                    # Application code
│   ├── Http/              # HTTP controllers and middleware
│   │   └── Controllers/   # Application controllers
│   ├── Models/            # Eloquent models
│   └── ...
├── bootstrap/             # Framework bootstrap files
├── config/                # Configuration files
├── database/
│   ├── migrations/        # Database migration files
│   ├── factories/         # Model factories for testing
│   └── seeders/           # Database seeders
├── public/                # Public assets (CSS, JS, images)
├── resources/
│   ├── views/             # Blade template files
│   ├── css/               # Stylesheets
│   └── js/                # JavaScript files
├── routes/                # Route definitions
│   ├── web.php            # Web routes
│   └── api.php            # API routes (if applicable)
├── storage/               # Logs, uploads, caches
├── tests/                 # Test files
├── .env.example           # Environment variable template
├── composer.json          # PHP dependencies
├── package.json           # Node dependencies
├── artisan                # Laravel CLI
└── vite.config.js         # Vite configuration

Configuration

Database Configuration

Configure your database in the .env file:

  • DB_CONNECTION: Database driver (mysql, sqlite, pgsql)
  • DB_DATABASE: Database name
  • DB_USERNAME: Database user
  • DB_PASSWORD: Database password

Application Configuration

Other important .env settings:

  • APP_NAME: Application name
  • APP_ENV: Environment (local, production)
  • APP_DEBUG: Debug mode (true/false)
  • APP_URL: Application URL

Database Setup

Create Database

mysql -u root -p
CREATE DATABASE laravel_crud;
EXIT;

Run Migrations

Migrations define the database schema:

php artisan migrate

Run Seeders (Optional)

Populate database with sample data:

php artisan db:seed

Rollback Migrations

To undo migrations:

php artisan migrate:rollback

Running the Application

Development Server

Start Laravel’s built-in development server:

php artisan serve

The application will be accessible at http://localhost:8000

Build Assets

Compile frontend assets with Vite:

npm run dev      # Development mode with hot reload
npm run build    # Production build

API Endpoints

Standard CRUD Endpoints

MethodEndpointActionDescription
GET/itemsindexDisplay all items
GET/items/{id}showDisplay single item
GET/items/createcreateShow create form
POST/itemsstoreStore new item in database
GET/items/{id}/editeditShow edit form
PUT/PATCH/items/{id}updateUpdate item in database
DELETE/items/{id}destroyDelete item from database

Note: Replace items with your actual resource name


CRUD Operations

Create (C)

To create a new record:

  1. Access the create form: GET /items/create
  2. Submit form data: POST /items
  3. Redirects to show page after successful creation

Read (R)

Retrieve records:

  • List all: GET /items → displays all records
  • View single: GET /items/{id} → shows specific record

Update (U)

Modify existing records:

  1. Access edit form: GET /items/{id}/edit
  2. Submit changes: PUT/PATCH /items/{id}
  3. Database updated and redirects to show page

Delete (D)

Remove records:

  • Send DELETE /items/{id} request
  • Record removed from database
  • Redirects to list page

File Naming Conventions

  • Controllers: Singular, PascalCase (e.g., ItemController.php)
  • Models: Singular, PascalCase (e.g., Item.php)
  • Migrations: Snake_case with timestamp (e.g., 2024_01_15_create_items_table.php)
  • Views: Lowercase with dots for nesting (e.g., items.index, items.create)
  • Routes: RESTful naming convention

Common Artisan Commands

# Generate new controller
php artisan make:controller ItemController --resource

# Create a model with migration
php artisan make:model Item -m

# Create migration
php artisan make:migration create_items_table

# List all routes
php artisan route:list

# Clear cache
php artisan cache:clear

# Run tests
php artisan test

# Generate IDE helper for better autocomplete
php artisan ide-helper:generate

Troubleshooting

Database Connection Error

  • Verify database credentials in .env
  • Ensure database server is running
  • Check database name exists

Migration Errors

# Check migration status
php artisan migrate:status

# Rollback and retry
php artisan migrate:rollback
php artisan migrate

Missing Key Error

php artisan key:generate

Asset Not Found

npm install
npm run dev

Permission Denied Errors

chmod -R 775 storage bootstrap/cache

Port Already in Use

# Use different port
php artisan serve --port=8001

Best Practices

  1. Always validate user input in controllers and forms
  2. Use meaningful variable names for clarity
  3. Follow Laravel naming conventions consistently
  4. Write database queries using Eloquent ORM when possible
  5. Use migrations for database schema changes
  6. Implement proper error handling and logging
  7. Write tests for critical functionality
  8. Keep controllers lean - move logic to models or services
  9. Use environment variables for sensitive data
  10. Regularly commit changes with clear messages

Learning Resources


Support & Contributions

For questions or issues, please refer to the Laravel Documentation or open an issue in the repository.

To contribute to this learning project, fork the repository and submit a pull request with your improvements.


License

This project is open-sourced software licensed under the MIT license.