如何在laravel中获得订单关系

如何在laravel中获得订单关系,laravel,relationship,Laravel,Relationship,我有一个亲戚 Order(id, user_id, total_price) 提问:如何通过模板获取订单: 顺序:[ id:1, 用户id:1, 总价:1.1, 订单详情:[ id:1, 订单号:1, 图书编号:1, 产品数量:10 产品:[ id:1, 标题:“abc”, 存在的数量:100, 价格:0.1 ] ] ] 您可以通过laravel eloquent relationship来定义关系,您将有3个模型,如订单、订单详细信息、与相应表相关的产品 在您的订购模型上: publ

我有一个亲戚

Order(id, user_id, total_price)
提问:如何通过模板获取订单:

顺序:[
id:1,
用户id:1,
总价:1.1,
订单详情:[
id:1,
订单号:1,
图书编号:1,
产品数量:10
产品:[
id:1,
标题:“abc”,
存在的数量:100,
价格:0.1
]
]
]

您可以通过laravel eloquent relationship来定义关系,您将有3个模型,如订单、订单详细信息、与相应表相关的产品

在您的订购模型上:

    public function OrderDetail()
    {
        return $this->hasOne('App\OrderDetail','order_id');
    }
在您的OrderDetail模型上:

    public function Order()
    {
        return $this->belongsTo('App\Order','order_id');
    }
    public function Product()
    {
        return $this->belongsTo('App\Product','product_id');
    }

然后在你的控制器上

    public function foo(){
        $orders=Order::all(); //remember to use 'App\Order'
        return view('filename',compact('orders'));
    }
现在,在刀片上,您只需链接订单即可找到所有相关的表数据

    @foreach ($orders as $order)
        @if ($order->OrderDetail)
            @if($order->OrderDetail->Product)
                {{$order->OrderDetail->Product->title}}
            @endif
        @endif
    @endforeach

有关更多信息,请参见

您可以通过laravel eloquent relationship来实现,要定义关系,您将有3个模型,如订单、订单详细信息、与相应表相关的产品

在您的订购模型上:

    public function OrderDetail()
    {
        return $this->hasOne('App\OrderDetail','order_id');
    }
在您的OrderDetail模型上:

    public function Order()
    {
        return $this->belongsTo('App\Order','order_id');
    }
    public function Product()
    {
        return $this->belongsTo('App\Product','product_id');
    }

然后在你的控制器上

    public function foo(){
        $orders=Order::all(); //remember to use 'App\Order'
        return view('filename',compact('orders'));
    }
现在,在刀片上,您只需链接订单即可找到所有相关的表数据

    @foreach ($orders as $order)
        @if ($order->OrderDetail)
            @if($order->OrderDetail->Product)
                {{$order->OrderDetail->Product->title}}
            @endif
        @endif
    @endforeach

有关更多信息,请参见在您的
订单中创建关系

public function order_details()
{
    return $this->hasOne('App\OrderDetail','order_id','id');
}
在您的
Orderdetail
模型中

public function product_details()
{
    return $this->belongsTo('App\Product','id','product_id'); // first foreign key then local key
}
在控制器中

$orders = Order::all(); 
return view('view_name',compact('orders'));
在视图中

@foreach ($orders as $order)
    @if ($order->order_details)
        {{$order->order_details->num_of_product}}
        @if($order->order_details->product_details)
            {{$order->order_details->product_details->title}}
            {{$order->order_details->product_details->num_of_existed}}
            {{$order->order_details->product_details->price}}
        @endif
    @endif
@endforeach

在您的
订单中创建关系

public function order_details()
{
    return $this->hasOne('App\OrderDetail','order_id','id');
}
在您的
Orderdetail
模型中

public function product_details()
{
    return $this->belongsTo('App\Product','id','product_id'); // first foreign key then local key
}
在控制器中

$orders = Order::all(); 
return view('view_name',compact('orders'));
在视图中

@foreach ($orders as $order)
    @if ($order->order_details)
        {{$order->order_details->num_of_product}}
        @if($order->order_details->product_details)
            {{$order->order_details->product_details->title}}
            {{$order->order_details->product_details->num_of_existed}}
            {{$order->order_details->product_details->price}}
        @endif
    @endif
@endforeach

你创造了他们之间的关系吗?你创造了他们之间的关系吗?