Skip to content

Commit

Permalink
Merge pull request #40 from notverygood135/orders
Browse files Browse the repository at this point in the history
[Task #80533] Create order history page
  • Loading branch information
thinhnv-2059 authored Sep 4, 2024
2 parents 6bd6596 + a2e7b10 commit a1e0382
Show file tree
Hide file tree
Showing 17 changed files with 982 additions and 105 deletions.
80 changes: 80 additions & 0 deletions ecommerce-app/app/Http/Controllers/OrderDetailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Http\Controllers;

use App\Models\OrderDetail;
use App\Http\Controllers\Controller;
use App\Models\OrderItem;
use App\Models\Product;
use Illuminate\Http\Request;

class OrderDetailController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$orders = OrderDetail::all();
return view('order-details.index', [
'orders' => $orders,
]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}

/**
* Display the specified resource.
*/
public function show(OrderDetail $orderDetail)
{
// $orderItems = OrderItem::join('products', 'order_items.product_id', '=', 'products.id')
// ->where('order_detail_id', $orderDetail->id)
// ->get(['order_items.*', 'products.name', 'products.price', 'products.image_url']);

$orderItems = OrderItem::with('product')->where('order_detail_id', $orderDetail->id)->get();

return view('order-details.show', [
'orderDetail' => $orderDetail,
'orderItems' => $orderItems,
]);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(OrderDetail $orderDetail)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, OrderDetail $orderDetail)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(OrderDetail $orderDetail)
{
//
}
}
2 changes: 1 addition & 1 deletion ecommerce-app/app/Http/Controllers/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function create()
return view('admin.products.create', compact('categories'));
}

public function store(Request $request)
public function store(ProductRequest $request)
{
$product = new Product($request->validated());

Expand Down
2 changes: 1 addition & 1 deletion ecommerce-app/app/Http/Requests/CategoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CategoryRequest extends FormRequest
*/
public function authorize(): bool
{
return false;
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ecommerce-app/app/Http/Requests/ProductRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ProductRequest extends FormRequest
*/
public function authorize(): bool
{
return false;
return true;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions ecommerce-app/app/Models/OrderDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class OrderDetail extends Model
{
Expand All @@ -20,4 +22,13 @@ public static function booted(): void
$orderDetail->id = Str::uuid();
});
}

protected $casts = [
'id' => 'string'
];

public function orderItems(): HasMany
{
return $this->hasMany(OrderItem::class);
}
}
10 changes: 8 additions & 2 deletions ecommerce-app/app/Models/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Str;

Expand All @@ -16,9 +17,14 @@ public function userReview(): HasOne
return $this->hasOne(UserReview::class);
}

public function product(): HasOne
public function product(): BelongsTo
{
return $this->hasOne(Product::class, 'product_id');
return $this->belongsTo(Product::class, 'product_id');
}

public function orderDetail(): BelongsTo
{
return $this->belongsTo(OrderDetail::class, 'order_detail_id');
}

public $incrementing = false;
Expand Down
5 changes: 3 additions & 2 deletions ecommerce-app/app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -44,9 +45,9 @@ public function carts(): BelongsToMany
return $this->belongsToMany(ShoppingCart::class, 'shopping_cart_items', 'product_id', 'shopping_cart_id');
}

public function orderItems(): BelongsToMany
public function orderItems(): HasMany
{
return $this->belongsToMany(OrderItem::class);
return $this->hasMany(OrderItem::class);
}

public function userReviews(): HasManyThrough
Expand Down
Loading

0 comments on commit a1e0382

Please sign in to comment.