0

我想将订单和产品(annonce)发送到Mailtrap,我尝试了id = 1的订单作为测试,发送时一切正常,只需给他们未显示在Mailtrap中的产品(annonce) ,另一方面,给他们下订单会很好地显示出来。我不知道问题出在哪里。以及为什么没有显示产品(公告)信息。

网页.php

Route::get('/mailable', function(){
    $order = App\Order::find(1);
    //dd($order);
    return new App\Mail\OrderPlaced($order);
});

OrderAnnonce.php

class OrderAnnonce extends Model
{
    protected $table = 'order_annonce';
    protected $fillable = ['order_id','annonce_id','quantity'];
} 

订单.php

class Order extends Model
{
    protected $fillable = [
        'user_id', 'billing_email', 'billing_name', 'billing_address', 'billing_city',
        'billing_province', 'billing_postalcode', 'billing_phone', 'billing_name_on_card', 'billing_discount',
         'billing_discount_code', 'billing_subtotal', 'billing_tax', 'billing_total', 'payment_gateway', 
         'error',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function annonces()
    {
        return $this->belongsToMany('App\Annonce')->withPivot('quantity');
    }  

放置.blade.php

Thank you for your order.

**Order ID:** {{ $order->id }}

**Order Email:** {{ $order->billing_email }}

**Order Name:** {{ $order->billing_name }}

**Order Total:** ${{ round($order->billing_total / 100, 2) }}

**Items Ordered**

@foreach($order->annonces as $annonce)
Name: {{ $annonce->name }} <br>
Price: ${{ round($annonce->price / 100, 2)}} <br>
Quantity: {{ $annonce->pivot->quantity }} <br>
@endforeach
4

1 回答 1

1

听起来像是您annonceorder模型之间的数据透视表。

多对多关系的文档中:“为了确定关系的中间表的表名,Eloquent 将按字母顺序连接两个相关的模型名。但是,您可以随意覆盖此约定。您可以通过传递belongsToMany 方法的第二个参数

在您的模型关系中,添加第二个参数“annonces_order”:

return $this->belongsToMany(Order::class, 'Annonces_order');

return $this->belongsToMany(Annonce::class, 'Annonces_order');

或者,将您的表重命名Annonces_orderannonce_order.


编辑:

由于您使用的是中间模型,因此您需要告诉您的关系实际使用该模型。添加->using(OrderAnnonce::class)到您的两个关系:

return $this->belongsToMany('App\Annonce')->using(OrderAnnonce::class)->withPivot('quantity');

更多关于定义自定义中间表模型

于 2021-10-24T17:36:08.293 回答