【发布时间】:2019-10-28 21:33:43
【问题描述】:
我正在尝试让买家看到的订单被相应的卖家看到,但它没有显示任何内容。
我已经尝试了几个小时来解决这个问题,但我仍然卡住了。
这是我的模型Order.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
//protected $table = 'orders';
protected $fillable = [
'user_id', 'shipping_email', 'shipping_name', 'shipping_city', 'shipping_phone', 'billing_subtotal', 'billing_total',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function products()
{
return $this->belongsToMany('App\Products_model')->withPivot('quantity');
}
public function orders(){
return $this->hasMany('App\OrderProduct', 'order_id');
}
}
这是我的 OrderProduct.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class OrderProduct extends Model
{
protected $table = 'order_product';
protected $fillable = ['order_id', 'product_id', 'quantity'];
public function products()
{
return $this->belongsTo('App\Products_model');
}
}
这是我的User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'Seller'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function products()
{
return $this->hasMany(Products_model::class);
}
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function orders()
{
$this->hasManyThrough(Order::class, Products_model::class, 'user_id', 'product_id');
}
}
最后是我在 ProductController
中的 viewOrder 函数//Orders View Function
public function viewOrders(User $user)
{
$products = Products_model::where('user_id', '=', $user->id)->get();
$orders = [];
foreach($products as $product){
array_merge($orders, $product->order);
}
//dd( $products);
return view('orders')->with(compact('orders'));
}
我需要每个列出产品的卖家(用户)在其他买家(用户)购买时收到订单。到目前为止它显示 "订单 ID 订单日期 客户姓名 客户城市 客户电话
【问题讨论】:
标签: php laravel eloquent laravel-5.8