Create master data
Now we have a model definition. Let's create master data. Usually there is a user interface to manage master data, but we omit it because it is not the essence in this example.
In this chapter, you can learn about the following contents:
- Create model with
create
method - Create model with
new
operator - How to use relationship method
associate
Customer
id
is auto-increment. Specify only name.
Customer::create(['name' => 'Suzuki']);
Customer::create(['name' => 'Honda']);
Customer::create(['name' => 'Yamaha']);
Customer::create(['name' => 'Kawasaki']);
Customer::create(['name' => 'Matsuda']);
Customer::create(['name' => 'Toyota']);
Product and Stock
Register the product and its stock quantity with small function.
function addProduct($code, $desc, $price, $qty)
{
$prod = Product::create(['code' => $code, 'description' => $desc, 'price' => $price]);
$stock = new Stock(['quantity' => $qty]);
$stock->product()->associate($prod); // Copy $prod->code to $stock->code
$stock->save();
}
addProduct('CAR123', 'CAR 123', 5000000, 1);
addProduct('BIKE650', 'BIKE 650', 320000, 5);
addProduct('WATCH777', 'WATCH 777', 198000, 10);
addProduct('APPLE1', 'APPLE FUJI', 300, 120);
addProduct('APPLE2', 'APPLE SHINANO', 280, 240);
addProduct('ORANGE1', 'ORANGE UNSYUU', 320, 54);
Now the master data has been created, you can create invoices.