Trước khi bắt tay vào công việc tạo các file chúng ta cần lưu ý
😒 Thứ tự thời gian tạo các file rất quan trọng (vì có các mối quan hệ ràng buộc trong bảng
do đó những bảng phụ phải được tạo trước)
😒 Cách lệnh được sử dụng (Thứ tự phải như sau)
$php artisan migrate
$php artisan db:seed --class=CountrySeeder
$php artisan db:seed --class=UserSeeder
$php artisan db:seed --class=PostSeeder
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
namespace Database\Factories;
use App\Models\Menurole;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(Menurole::class, function (Faker $faker) {
return [
'role_name' => 'guest',
'menus_id' => factory(App\Models\Menus::class)->create()->id,
];
});
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
namespace Database\Factories;
use App\Models\Menus;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(Menus::class, function (Faker $faker) {
return [
'name' => $faker->sentence(4, true),
'href' => '/href',
'icon' => NULL,
'slug' => 'link',
'parent_id' => NULL,
'menu_id' => 1,
'sequence' => 1
];
});
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMenusTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('menus', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('href')->nullable();
$table->string('icon')->nullable();
$table->string('slug');
$table->integer('parent_id')->unsigned()->nullable();
$table->integer('menu_id')->unsigned();
$table->integer('sequence');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('menus');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMenuroleTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('menu_role', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('role_name');
$table->integer('menus_id')->unsigned();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('menu_role');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMenulistTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('menulist', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('menulist');
}
}
<?php
namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\User;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
User::factory(12)->create();
$this->call([
MenusTableSeeder::class
]);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Menulist extends Model
{
protected $table = 'menulist';
public $timestamps = false;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Menurole extends Model
{
protected $table = 'menu_role';
public $timestamps = false;
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Menus extends Model
{
protected $table = 'menus';
public $timestamps = false;
}
<?php
namespace Database\Factories;
use App\Models\Country;
use Illuminate\Database\Eloquent\Factories\Factory;
class CountryFactory extends Factory {
protected $model = Country::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'name' => $this->faker->text(10),
];
}
}
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory {
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'name' => $this->faker->text(10),
'user_id' => $this->faker->numberBetween(1, 10)
];
}
}
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class UserFactory extends Factory {
/**
* Define the model's default state.
*
* @return array
*/
public function definition() {
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'country_id' => $this->faker->numberBetween(1, 10),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
/**
* Indicate that the model's email address should be unverified.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
public function unverified() {
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCountriesTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('countries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('countries');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->bigInteger('country_id')->unsigned();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('users');
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name");
$table->bigInteger('user_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists('posts');
}
}
<?php
namespace Database\Seeders;
use App\Models\Country;
use Illuminate\Database\Seeder;
class CountrySeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Country::factory()->count(10)->create();
}
}
<?php
namespace Database\Seeders;
use App\Models\Post;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
Post::factory()->count(10)->create();
}
}
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run() {
User::factory(10)->create();
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Country extends Model {
use HasFactory;
protected $table = 'countries';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {
use HasFactory;
protected $fillable = [
'name', 'user_id'
];
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable {
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'country_id',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
<?php
namespace App\Http\Controllers;
use App\Models\Country;
class TestController extends Controller {
public function show() {
$country = Country::find(1);
$posts = $country->posts;
return view('test')->with(compact('posts'));
}
}
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/test', [App\Http\Controllers\TestController::class, 'show'])->name('home');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');