Commit 78eea069 authored by Artem's avatar Artem

add order details table

parent c52aeea3
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace App; namespace App;
//use Illuminate\Database\Eloquent\Model; //use Illuminate\Database\Eloquent\Model;
use App\Product;
class Cart /*extends Model*/ class Cart /*extends Model*/
{ {
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Order; use App\Order;
use App\OrderDetail;
use App\Product;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Session; use Session;
...@@ -25,6 +27,19 @@ class OrderController extends Controller ...@@ -25,6 +27,19 @@ class OrderController extends Controller
$order_id = $order->id; $order_id = $order->id;
foreach ($cart->items as $id => $item) {
$product = Product::where('id', $id)->first();
OrderDetail::create([
'order_id' => $order_id,
'product_id' => $id,
'ordered_reference' => $product->reference,
'ordered_price' => $product->price,
'ordered_qty' => $item['qty'],
'subtotal_price' => $product->price * $item['qty'],
]);
}
Session::forget('cart'); Session::forget('cart');
return view('cart', compact('order_id')); return view('cart', compact('order_id'));
......
...@@ -12,4 +12,9 @@ class Order extends Model ...@@ -12,4 +12,9 @@ class Order extends Model
{ {
return $this->belongsTo(User::class); return $this->belongsTo(User::class);
} }
public function products()
{
return $this->hasMany(OrderDetail::class, 'order_id');
}
} }
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderDetail extends Model
{
protected $fillable = ['order_id', 'product_id', 'ordered_reference', 'ordered_price', 'ordered_qty', 'subtotal_price'];
}
...@@ -16,7 +16,7 @@ $factory->define(Product::class, function (Faker $faker) { ...@@ -16,7 +16,7 @@ $factory->define(Product::class, function (Faker $faker) {
'reference' => 'Product ' . $product_id++, 'reference' => 'Product ' . $product_id++,
'description_short' => $faker->sentence, 'description_short' => $faker->sentence,
'description_long' => $faker->paragraph, 'description_long' => $faker->paragraph,
'price' => $faker->randomNumber(4), 'price' => $faker->randomNumber(3),
'type' => $product_type[rand(1, 2)], 'type' => $product_type[rand(1, 2)],
]; ];
}); });
...@@ -15,9 +15,9 @@ class CreateOrdersTable extends Migration ...@@ -15,9 +15,9 @@ class CreateOrdersTable extends Migration
{ {
Schema::create('orders', function (Blueprint $table) { Schema::create('orders', function (Blueprint $table) {
$table->id(); $table->id();
$table->unsignedBigInteger('user_id'); $table->foreignId('user_id');
$table->string('total_price'); $table->integer('total_price');
$table->string('total_qty'); $table->integer('total_qty');
$table->timestamps(); $table->timestamps();
$table->foreign('user_id') $table->foreign('user_id')
......
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrderDetailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('order_details', function (Blueprint $table) {
$table->id();
$table->foreignId('order_id');
$table->foreignId('product_id');
$table->string('ordered_reference');
$table->integer('ordered_price');
$table->integer('ordered_qty');
$table->integer('subtotal_price');
$table->timestamps();
$table->foreign('order_id')
->references('id')
->on('orders')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('order_details');
}
}
...@@ -15,9 +15,7 @@ class UserSeeder extends Seeder ...@@ -15,9 +15,7 @@ class UserSeeder extends Seeder
User::create([ User::create([
'name' => 'Artem', 'name' => 'Artem',
'email' => 'ololo@info.com', 'email' => 'ololo@info.com',
'email_verified_at' => now(), 'password' => Hash::make('123123123'),
'password' => '123123123',
'remember_token' => Str::random(10),
]); ]);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment