Laravel:如何使用存储库文件显示数据库中的信息

时间:2020-09-03 05:33:03

标签: php laravel repository

我正在尝试向我的Laravel应用程序添加购物车功能。我主要按照此网站(Laravel E-Commerce Application Development – CheckoutOrder Management)中的说明进行了一些更改,以匹配我想要的内容。

我已经成功地收到了客户的订单并将记录添加到数据库中,但是我不知道如何显示订单明细。在我的数据库中,我有用户表,产品表,订单表和order_item表(请参见上面提到的网站中每个表的结构)。产品表属于user_table,order_item表属于product表,而order表属于order_item表。

我想做的是,我希望每个登录的用户(商店)都能在他们的帐户中看到客户所订购的产品。为此,我认为我需要在OrderController的show方法中添加类似$orders = Auth::user->products->order_item->orders的内容,但是有一个存储库介入其中,我被卡在这里。

我最近开始学习Laravel,但我对存储库的概念并不真正熟悉。任何帮助将不胜感激,因为我尝试了多种方法但均未成功。

谢谢。

web.php

<?php

use Illuminate\Support\Facades\Route;

Route::group(['middleware' => 'auth'], function () {

    Route::get('/{order}/show', 'OrderController@show');

});

OrderController.php

<?php

namespace App\Http\Controllers;

use App\Contracts\OrderContract;
use App\Http\Controllers\BaseController;

class OrderController extends BaseController
{
    protected $orderRepository;

    public function __construct(OrderContract $orderRepository)
    {
        $this->orderRepository = $orderRepository;
    }

    public function show($orderNumber)
    {
        $order = $this->orderRepository->findOrderByNumber($orderNumber);

        $this->setPageTitle('Order Details', $orderNumber);
        return view('order_details', compact('order'));
    }
}

OrderRepository

<?php

namespace App\Repositories;

use Cart;
use App\Models\Order;
use App\Product;
use App\Models\OrderItem;
use App\Contracts\OrderContract;

class OrderRepository extends BaseRepository implements OrderContract
{
    public function __construct(Order $model)
    {
        parent::__construct($model);
        $this->model = $model;
    }

    public function storeOrderDetails($params)
    {
        $order = Order::create([
            'order_number'      =>  'ORD-' . strtoupper(uniqid()),
            'status'            =>  'pending',
            'grand_total'       =>  Cart::getSubTotal(),
            'item_count'        =>  Cart::getTotalQuantity(),
            'table_number'      =>  $params['table_number'],
            'name'              =>  $params['name'],
            'notes'             =>  $params['notes']
        ]);

        if ($order) {

            $items = Cart::getContent();

            foreach ($items as $item) {
                // A better way will be to bring the product id with the cart items
                // you can explore the package documentation to send product id with the cart
                $product = Product::where('name', $item->name)->first();

                $orderItem = new OrderItem([
                    'product_id'    =>  $product->id,
                    'quantity'      =>  $item->quantity,
                    'price'         =>  $item->getPriceSum()
                ]);

                $order->items()->save($orderItem);
            }
        }

        return $order;
    }

    public function listOrders(string $order = 'id', string $sort = 'desc', array $columns = ['*'])
    {
        return $this->all($columns, $order, $sort);
    }

    public function findOrderByNumber($orderNumber)
    {
        return Order::where('order_number', $orderNumber)->first();
    }
}

order_details.blade.php

@section('title') {{ $pageTitle }} @endsection
@section('content')
<div class="app-title">
    <div>
        <h1><i class="fa fa-bar-chart"></i> {{ $pageTitle }}</h1>
        <p>{{ $subTitle }}</p>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="tile">
            <section class="invoice">
                <div class="row mb-4">
                    @foreach($orders as $order)
                    <div class="col-6">
                        <h2 class="page-header"><i class="fa fa-globe"></i> {{ $order->order_number }}</h2>
                    </div>
                    <div class="col-6">
                        <h5 class="text-right">Date: {{ $order->created_at->toFormattedDateString() }}</h5>
                    </div>
                </div>
                <div class="row invoice-info">
                    <div class="col-4">Placed By
                        <address><strong>{{ $order->user->fullName }}</strong></address>
                    </div>                        
                    <div class="col-4">
                        <b>Order ID:</b> {{ $order->order_number }}<br>
                        <b>Amount:</b> {{ config('settings.currency_symbol') }}{{ round($order->grand_total, 2) }}<br>                           
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Qty</th>
                                    <th>Product</th>
                                    <th>SKU #</th>
                                    <th>Qty</th>
                                    <th>Subtotal</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($order->items as $item)
                                <tr>
                                    <td>{{ $item->id }}</td>
                                    <td>{{ $item->product->name }}</td>
                                    <td>{{ $item->product->sku }}</td>
                                    <td>{{ $item->quantity }}</td>
                                    <td>{{ config('settings.currency_symbol') }}{{ round($item->price, 2) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>
@endsection

0 个答案:

没有答案