• Bỏ qua primary navigation
  • Skip to main content
  • Bỏ qua primary sidebar

Thủ thuật thiết kế website

Chia sẻ kinh nghiệm thiết kế website

  • Trang chủ
  • Joomla
    • Thủ thuật Joomla
    • Joomla Extensions
    • Joomla Themes
  • WordPress
    • Thủ thuật WordPress
    • WordPress Plugins
    • WordPress Themes
  • PHP Framework
    • Codeigniter
    • Laravel
    • Laminas
  • App Developer
    • React Native
    • Flutter
  • SEO
  • Chia sẻ
  • Phần mềm
Bạn đang ở:Trang chủ / PHP Framework / Laravel / [Laravel Tutorials] Tổ chức theo dạng packages/modules – Phần 3: Load migrations, models, translations

[Laravel Tutorials] Tổ chức theo dạng packages/modules – Phần 3: Load migrations, models, translations

21/09/2023 - Administrator Để lại bình luận

Trong bài tiếp theo này mình sẽ hướng dẫn các bạn load migrations cho modules, sử dụng models và translations.

1. Load migrations.

Việc load migrations cho module cũng đơn giản thôi, các bạn chỉ việc thêm vào hàm boot của /core/demo/src/Providers/DemoServiceProvider.php

$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');

Chúng ta sẽ load migrations từ thư mục /core/demo/database/migrations, vậy nên hãy tạo thư mục này nhé.

Giờ hãy thử migration đầu tiên nào. Chạy lệnh:

php artisan make:migration create_demo_table --path=core/demo/database/migrations

Tham số –path là để migration được tạo ra trong đúng thư mục mà mình đã tạo nhé, các bạn có thêm tham số –table hay –create tuỳ ý như tạo migration bình thường.

Thêm cột name vào trong bảng demo cho nó vui, source file migration tạo ra sẽ như thế này.

<?php 

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateDemoTable extends Migration
{
    /** 
     * Run the migrations. 
     * @return void 
     */
    public function up()
    {
        Schema::create('demo', function (Blueprint $table) {
            $table->increments('id');
            $table->text('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     * @return void 
     */
    public function down()
    {
        Schema::dropIfExists('demo');
    }
}

Tiếp theo là thiết lập kết nối database trong .env và chạy php artisan migrate để tạo database thôi.

2. Tạo model.

Giờ chúng ta đã có bảng demo, hãy tạo model cho bảng này, tạo file /core/demo/src/Models/Demo.php với nội dung.

<?php 

namespace Botble\Demo\Models;

use Illuminate\Database\Eloquent\Model;

class Demo extends Model
{
    /**
     * @var string 
     */
    protected $table = 'demo';

    /**
     * @var array 
     */
    protected $fillable = ['name',];
}

Giờ test thử cái model này thôi.

Sửa lại cái /core/demo/src/Http/Controllers/DemoController.php 

<?php 

namespace Botble\Demo\Http\Controllers;

use App\Http\Controllers\Controller;
use Botble\Demo\Models\Demo;

class DemoController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function getIndex()
    {
        Demo::create(['name' => 'Demo ' . time()]);
        return view('botble-demo::index');
    }
}

Vậy là giờ khi bạn truy cập http://localhost:8000/demo thì nó sẽ tự tạo 1 record trong bảng demo, demo nhỏ cho nó nhanh 😀

3. Load translation

Phần này cũng tương đối đơn giản, đầu tiên hãy tạo file /core/demo/resources/lang/en/demo.php với nội dung.

<?php

return [
    'name' => 'Demo',
];

Để load translation này lên thì ta thêm trong hàm boot của /core/demo/src/Providers/DemoServiceProvider.php 

$this->loadTranslationsFrom(__DIR__ . '/../../resources/lang', 'botble-demo');

Vậy là xong, bây giờ để dùng translation thì ta chỉ cần gọi trans(‘botble-demo::demo.name’) chẳng hạn.

Trong bài này mình chỉ làm vài ví dụ đơn giản. Khá là dễ để xây dựng package cho riêng mình. Hi vọng các bạn thực hiện thành công 😀

Các bạn có thể xem các thay đổi so với phần trước tại đây: https://github.com/sangnguyenplus/laravel-package-demo/commit/a6f4aad3f9957e0c9d235ec3a8d34e164abd8b74

Nguồn : https://sangplus.com

0 0 đánh giá
Article Rating
Theo dõi
Đăng nhập
Thông báo của
guest
guest
0 Comments
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất
Phản hồi nội tuyến
Xem tất cả bình luận

Sidebar chính

LỜI NGỎ

Đây là blog cá nhân, cung cấp các thông tin, kiến thức và kinh nghiệm về lập trình và cuộc sống. Những bài viết được mình sưu tập từ nhiều nguồn, mọi chi tiết liên quan đến bản quyền xin vui lòng liên hệ qua email kairu2607@gmail.com ! Cám ơn rất nhiều.

Tìm kiếm

Thủ thuật Wordpress

T

Tạo trang chuyển hướng download cho WordPress

[flatsome ] Text Ticker (fade) For Top Bar In Flatsome Theme

[Flatsome] Text Ticker (Fade) for Top Bar in Flatsome Theme

Định Dạng ảnh Webp Là Gì ? Làm Thế Nào để Sử Dụng Webp Trên Wordpress 2024 Mới Nhất ?

Định dạng ảnh WebP là gì ? Làm thế nào để sử dụng WebP trên WordPress 2024 mới nhất ?

X

Xử Lý Lỗi Không Xem Được Giỏ Hàng Và Thanh Toán Woocommerce Website WordPress

Vì Sao Rank Math Vượt Trội – SEO WordPress 2023

Hướng Dẫn Ghi đè (override) Folder Inc Trong Child Theme Flatsome

Hướng dẫn ghi đè (override) folder INC trong child-theme Flatsome

Hướng Dẫn Quản Lý Trang Trong Website WordPress

Laravel

Một số câu hỏi câu hỏi phổ biến phỏng vấn tuyển dụng lập trình viên Laravel

Thiết kế cấu trúc folder HMVC cho Laravel

Tại sao lại sử dụng Laravel Service và Repository Pattern?

[Laravel 7] Tổ chức theo dạng Package/Module trong ứng dụng Laravel – P3: khai báo config, translation, helpers và migrations

[Laravel 7] Tổ chức theo dạng Package/Module trong ứng dụng Laravel – P2: Route và mô hình MVC

[Laravel 7] Tổ chức theo dạng Package/Module trong ứng dụng Laravel – P1: Giới thiệu và khởi tạo cấu trúc thư mục cơ bản

Codeigniter Framework

[CodeIgniter 4] Codeigniter 4 Remove Public and Index.php From URL

[CodeIgniter 4] How to upload Codeigniter 4 website on share hosting?

Sửa lỗi website Codeigniter 2.x không chạy được với PHP 7.x

[CodeIgniter 4] Sử dụng cURL trong CodeIgniter 4

[CodeIgniter 4] Sử dụng cache để tăng tốc website trong CodeIgniter 4

[CodeIgniter 4] Xử lý hình ảnh chuyên nghiệp trong CodeIgniter 4

[CodeIgniter 4] Hướng dẫn gửi mail trong CodeIgniter 4

Dịch vụ Thiết Kế Website

Phần mềm hay

Hướng dẫn chuyển đổi php version trong ~/.zshrc ở MacOs

Tim Hieu Ve He Dieu Hanh Macos 1

Sửa file Hosts trong hệ điều hành MacOS

Hướng dẫn sử dụng phần mềm putty trên Windows

Switching between multiple PHP versions on macOS

Byebye Edge Chromium/Microsoft Edge

Copyright © 2025 · Metro Pro on Genesis Framework · WordPress · Đăng nhập

wpDiscuz