Seeder
files used to populate the table
can be populated using class seeder and database seeder or factory and database seederFactory and Database SeederSeeder class DatabaseSeeder extends Seeder { public function run(): void { [ModelName]::factory(10)->create(); // it will the factory template 10 times } } Model prepare the model to be used by the factory by adding fillable properties protected $fillable = ['title','description','long_description']; Factory create a factory and link to the model using this code: php artisan make:factory TaskFactory -model=Task modify the definition() method with Faker in the fac
https://laravel.com/docs/11.x/database-testing#running-seeders
There are 3 ways to insert data:
<?php
namespace Database\Seeders;
use Carbon\Carbon;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class ClothesSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
//
// 3 methods:
// 1. DB::raw
// 2. DB::table
// 3. Model::
// 1. -> DB RAW -> Manual Query
// DB::raw("INSERT INTO USER BLABLABLA)
// DB::raw("SELECT * FROM USERS)
// 2. -> Query Builder -> Built in laravel
// DB::table("users)->insert(['name'=> blabal])
// DB::table('users')->get();
// 3. -> Eloquent - ORM (Object Relational mapping)
// User::insert([]);
// User:all();
Clothes::insert([
'name' => 'Uniqlo Airism Oversized',
'price'=> '200000',
'created_at'=> Carbon::now(),
'updated_at'=> Carbon::now()
]);
}
}
Status: #idea
Tags: web-programming > web-prog-mid-exam-materials Mid Exam MaterialsWeb Programming (Laravel)Mid Exam Materials
Final Exam Materials
Status: #MOC
Tags:, web-prog-mid-exam-materialsweb-prog-mid-exam-materialsWP-Mid-Exam-Materials
my-wp-uts-practice
Initialization
composer create-project laravel/laravel newProject
not necessary since no wifi and project might be already given
Database
Setup Database
1. Open .env file and look for DB_.. and make sure its the same as this
DB_CONNECTION=mysql // XAMPP runs mysql database engine
DB_HOST=127.0.0.1
DB_PORT=3306 // port for database
DB_DATABASE=laravelproject // make a new database name
DB_USERNAME=root // credentials for XAMPP
DB_PASSWORD= // empty