Integrasi Template Bootstrap Dengan Laravel CRUD Part 1
3 min read
5 months ago
Published on Aug 26, 2024
This response is partially generated with the help of AI. It may contain inaccuracies.
Table of Contents
Introduction
This tutorial will guide you through the process of integrating a Bootstrap template with Laravel to create a CRUD (Create, Read, Update, Delete) application. We will be using the SB Admin template, which can be downloaded from the provided link. By the end of this tutorial, you will have a functional Laravel application that displays data using the Bootstrap template.
Step 1: Download the Bootstrap Template
- Visit the SB Admin template page at Start Bootstrap.
- Click on the download button to get the template files.
- Extract the downloaded ZIP file to a directory on your local machine.
Step 2: Set Up Laravel Project
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your new Laravel project.
- Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel your-project-name
- Change into the project directory:
cd your-project-name
Step 3: Configure the Database
- Open the
.env
file in the root directory of your Laravel project. - Update the following lines with your database configuration:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=your_database_name DB_USERNAME=your_username DB_PASSWORD=your_password
- Save the changes.
Step 4: Create a Migration for CRUD
- Run the following command to create a migration file:
php artisan make:migration create_items_table
- Open the migration file located in
database/migrations/
. - Define the structure of your table in the
up
method. For example:public function up() { Schema::create('items', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description'); $table->timestamps(); }); }
- Run the migration to create the table:
php artisan migrate
Step 5: Create a Model for the Database
- Run the following command to create a model:
php artisan make:model Item
- Open the newly created model in
app/Models/Item.php
and ensure it looks like this:namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Item extends Model { use HasFactory; protected $fillable = ['name', 'description']; }
Step 6: Set Up Routes
- Open
routes/web.php
. - Define the routes for the CRUD operations:
Route::resource('items', ItemController::class);
Step 7: Create a Controller
- Run the following command to create a controller:
php artisan make:controller ItemController --resource
- In
app/Http/Controllers/ItemController.php
, implement the necessary methods for handling CRUD operations.
Step 8: Integrate Bootstrap Template
- Copy the relevant Bootstrap CSS and JS files from the downloaded SB Admin template into the
public
directory of your Laravel project. - In the
resources/views
directory, create a new Blade file (e.g.,index.blade.php
). - Include the Bootstrap files in your Blade template:
<link href="{{ asset('css/sb-admin-2.min.css') }}" rel="stylesheet"> <script src="{{ asset('js/sb-admin-2.min.js') }}"></script>
Conclusion
You have successfully set up a Laravel project integrated with a Bootstrap template to perform CRUD operations. You can now customize your application further, add more features, and experiment with different Bootstrap components. Next steps may include implementing advanced features like user authentication or adding AJAX for a better user experience.