Created
July 26, 2023 15:58
-
-
Save NandoKstroNet/c89a258bdb9e9443f02de8a8fb867600 to your computer and use it in GitHub Desktop.
Conteúdo Acessório Área de Pedidos - Admin Filament
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#up | |
Schema::create('order_items', function (Blueprint $table) { | |
$table->id(); | |
$table->foreignId('order_id') | |
->constrained('user_orders') | |
->cascadeOnDelete(); | |
$table->foreignId('product_id') | |
->constrained() | |
->cascadeOnDelete(); | |
$table->integer('amount'); | |
$table->integer('order_value'); | |
$table->timestamps(); | |
}); | |
#down | |
Schema::dropIfExists('order_items'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#up | |
Schema::create('user_orders', function (Blueprint $table) { | |
$table->id(); | |
$table->uuid('order_code'); | |
$table->foreignId('user_id')->constrained()->cascadeOnDelete(); | |
$table->timestamps(); | |
}); | |
#down | |
Schema::dropIfExists('user_orders'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
#up seed | |
$user = \App\Models\User::factory() | |
->hasOrders(1) | |
->create(); | |
$order = $user->orders->first(); | |
foreach (\App\Models\Product::orderByRaw('RANDOM()')->take(4)->get() as $prod) { | |
$amount = rand(1, 5); | |
$order->items()->create([ | |
'product_id' => $prod->id, | |
'amount' => $amount, | |
'order_value' => $prod->price * $amount | |
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use Illuminate\Database\Eloquent\Factories\HasFactory; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\Relations\BelongsTo; | |
class UserOrder extends Model | |
{ | |
use HasFactory; | |
public function items() | |
{ | |
return $this->hasMany(OrderItem::class, 'order_id'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Database\Factories; | |
use Illuminate\Database\Eloquent\Factories\Factory; | |
/** | |
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\UserOrder> | |
*/ | |
class UserOrderFactory extends Factory | |
{ | |
/** | |
* Define the model's default state. | |
* | |
* @return array<string, mixed> | |
*/ | |
public function definition(): array | |
{ | |
return [ | |
'order_code' => $this->faker->uuid() | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment