Eloquent
Eloquent is an object-relational mapper (ORM) supported by default in Laravel framework to make it enjoyable when interacting with your database. For more info, you may like to read Eloquent ORM first.
Migrations
Migrations, like version control for your database, allow your team to define and share the application's database schema definitions. With database migrations, you no longer have to tell teammates to manually update required changes in their local database schema after pulling certain commits from source control.
Migrations classes are under src/Database/Migrations
directory.
packages/metafox/blog/src/: Package source rootDatabase/Migrations/2021_02_04_034457_CreateBlogTables.php : Migrations for blog schema2021_02_05_034457_CreateCategoryTables.php : Migrations for category schema
Each Migrations class is child of Illuminate\Database\Migrations\Migration
class, and contains 2 up
and down
methods. Let's see the Migration CreateBlogTables
class below
<?phpuse MetaFox\Platform\Support\DbTableHelper;use Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateBlogTables extends Migration{/*** Run the migrations.** @return void*/public function up(){if (!Schema::hasTable('blogs')) {Schema::create('blogs', function (Blueprint $table) {$table->bigIncrements('id');// other columns});}// setup other table}/*** Reverse the migrations.** @return void*/public function down(){DbTableHelper::dropStreamTables('blog');}}
MetaFox platform is shipped with DbTableHelper, to support creating platform schemas easily and quickly.
For more info, please read Laravel migrations
Seeders
Each package includes the ability to seed your database with default data using Seeder classes.
All package Seeder classes are stored in the src/Database/Seeders
directory. By default, a DatabaseSeeder
class is defined for you. With this class, you may use the call
method to run other Seeder classes. It will allow you to control the seeding order.
<?phpnamespace MetaFox\Blog\Database\Seeders;use Illuminate\Database\Seeder;class PackageSeeder extends Seeder {public function run(){// seed data to database}}
Factories
Please read Define Laravel Model Factories
Observers
If your app is listening for many events on a given Model, you may use Observer to group all of your listeners into a single class. Observer classes have method names reflecting on the Eloquent events you wish to listen to. Each of these methods receives the affected Model as their only one argument.
<?php// file name packages/metafox/blog/src/Observers/BlogObserver.phpnamespace MetaFox\Blog\Observers;use Exception;use MetaFox\Blog\Models\Blog;/*** Class BlogObserver.*/class BlogObserver{/*** Invoked when a model is creating** @param Model $model*/public function creating(Model $model){}/*** Invoked when a model created** @param Model $model*/public function created(Model $model){}/*** Invoked when a model is updating** @param Model $model*/public function updating(Model $model){}/***Invoked when a model is updated** @param Model $model*/public function updated(Model $model){}/*** Invoked when a model is deleting** @param Model $model*/public function deleting(Model $model){}/*** Invoked when a model deleted** @param Model $model*/public function deleted(Model $model): void{}}
For performance optimization, MetaFox does not support automatically discovering Observers, you must register Observers
within the package ServiceProvider class. Below is the sample BlogServiceProvider
class
<?phpnamespace MetaFox\Blog\Providers;use MetaFox\Platform\Support\EloquentModelObserver;use Illuminate\Support\ServiceProvider;use MetaFox\Blog\Models\Blog;use MetaFox\Blog\Observers\BlogObserver;class BlogServiceProvider extends ServiceProvider{/*** Boot the application events.** @return void*/public function boot(){Blog::observe([EloquentModelObserver::class, BlogObserver::class]);}}
For more info about Observers, please visit Laravel Model Observers
For Events list, please read Appendix - Event list
Repository
The main idea to use Repository Pattern in a Laravel is to create a bridge between models and controllers.