如何在Laravel 5.4中显示用户订单历史记录

时间:2017-08-18 09:33:31

标签: php laravel laravel-5 e-commerce

我有2张订单表
订单表

Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->string('fname');
            $table->string('lname')->nullable();
            $table->string('address1');
            $table->string('address2')->nullable();
            $table->string('city');
            $table->string('region');
            $table->string('postcode');
            $table->string('country');
            $table->string('phone');
            $table->string('total');
            $table->string('paymentmode');
            $table->string('randomid');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('restrict')
                        ->onUpdate('restrict');
            $table->timestamps();
        });

和制作人

Schema::create('productorders', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('qty');
            $table->integer('order_id')->unsigned();
            $table->foreign('order_id')->references('id')->on('orders')
                        ->onDelete('cascade')
                        ->onUpdate('restrict');
            $table->timestamps();
        });

这是我的订单模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class order extends Model
{   
    public function user(){

      return $this->belongsTo('App\User');

    }

    public function order(){

         return $this->hasMany('App\productorder');

    }
}

这里是生产者模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class productorder extends Model
{
    public function order(){

      return $this->belongsTo('App\order');

    }
}

订单有许多生产商和生产商属于订单,因为每个订单都有多个产品

订单已成功完成,但现在我想显示用户订购的订单历史记录

感谢

1 个答案:

答案 0 :(得分:1)

在您的用户模型中添加以下内容:

Auth::user()->orders();

现在,您可以从控制器拨打

{{1}}

这将为用户返回Orders集合。然后您可以根据需要将其压缩到视图和格式。 您还可以使用急切/延迟加载来获取订单上的其他详细信息。 您可以在此处找到有关的更多信息:https://laravel.com/docs/5.4/eloquent-relationships