Bläddra i källkod

Create models and relationships for orders

It includes seeds and factories to make testing the UI easier, and the
order of migrations needed to be changed to prevent conflicts.
tags/v0.1.0
Immanuel Onyeka 3 år sedan
förälder
incheckning
2a3e48820a
7 ändrade filer med 158 tillägg och 2 borttagningar
  1. +25
    -0
      app/Models/Order.php
  2. +21
    -0
      app/Models/Service.php
  3. +5
    -0
      app/Models/User.php
  4. +38
    -0
      database/factories/OrderFactory.php
  5. +37
    -0
      database/migrations/2021_05_18_184617_create_services_table.php
  6. +2
    -1
      database/migrations/2021_05_19_185302_create_orders_table.php
  7. +30
    -1
      database/seeders/DatabaseSeeder.php

+ 25
- 0
app/Models/Order.php Visa fil

@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\User;
use App\Models\Service;

class Order extends Model
{
use HasFactory;

protected $fillable = [
'quantity'
];

public function service() {
return $this->belongsTo(Service::class);
}

public function user() {
return $this->belongsTo(User::class);
}
}

+ 21
- 0
app/Models/Service.php Visa fil

@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Order;

class Service extends Model
{
use HasFactory;

protected $fillable = [
'name', 'type', 'site', 'available'
];

public function order() {
return $this->hasMany(Order::class);
}

}

+ 5
- 0
app/Models/User.php Visa fil

@@ -6,6 +6,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\Order;

class User extends Authenticatable implements MustVerifyEmail
{
@@ -40,4 +41,8 @@ class User extends Authenticatable implements MustVerifyEmail
protected $casts = [
'email_verified_at' => 'datetime',
];

public function orders() {
return $this->hasMany(Order::class);
}
}

+ 38
- 0
database/factories/OrderFactory.php Visa fil

@@ -0,0 +1,38 @@
<?php

namespace Database\Factories;

use App\Models\Order;
use App\Models\User;
use App\Models\Service;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Log;

class OrderFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Order::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'quantity' => $this->faker->randomNumber(4, false),
'service_id' => function() {
return Service::inRandomOrder()->first()->id;
},
'user_id' => function() {
return User::inRandomOrder()->first()->id;
},
];
}

}

+ 37
- 0
database/migrations/2021_05_18_184617_create_services_table.php Visa fil

@@ -0,0 +1,37 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Models\Service;

class CreateServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('name');
$table->string('type')->nullable();
$table->string('site');
$table->boolean('available');
});

}

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

database/migrations/2021_05_13_185302_create_orders_table.php → database/migrations/2021_05_19_185302_create_orders_table.php Visa fil

@@ -16,7 +16,8 @@ class CreateOrdersTable extends Migration
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('product');
$table->foreignId('service_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->bigInteger('quantity');
});
}

+ 30
- 1
database/seeders/DatabaseSeeder.php Visa fil

@@ -4,6 +4,8 @@ namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Order;
use App\Models\Service;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
@@ -15,7 +17,31 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
User::create([
Service::create([
'name' => 'youtube_views',
'type' => 'views',
'site' => 'youtube',
'available' => true,
]);
Service::create([
'name' => 'instagram_views',
'type' => 'views',
'site' => 'instagram',
'available' => true,
]);
Service::create([
'name' => 'spotify_plays',
'type' => 'plays',
'site' => 'spotify',
'available' => true,
]);
Service::create([
'name' => 'twitter_likes',
'type' => 'likes',
'site' => 'spotify',
'available' => false,
]);
$test_user = User::create([
'name' => 'test_user_unverified',
'email' => 'unverified@example.com',
'role' => 'client',
@@ -39,5 +65,8 @@ class DatabaseSeeder extends Seeder
'active' => true,
'password' => Hash::make("test123")
]);

Order::factory()->count(15)->for($test_user)->create();
Order::factory()->count(25)->create();
}
}

Laddar…
Avbryt
Spara