0

I want to send the give of an order and also of product(annonce) to Mailtrap, I tried by order whose id = 1 as a test, when sending, everything works fine, just give them of product(annonce) not displayed in Mailtrap, on the other hand giving them an order is displayed well. I don't know exactly where the problem is. and why the product(annonce) information is not showing.

web.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'];
} 

Order.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');
    }  

placed.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
eee aaa
  • 181
  • 6

1 Answers1

1

Sounds like a pivot table between your annonce and order models.

From the documentation on Many To Many Relationships:"To determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order. However, you are free to override this convention. You may do so by passing a second argument to the belongsToMany method"

In your models relations, add a second parameter 'annonces_order':

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

and

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

Or, rename your table Annonces_order to annonce_order.


Edit:

Since you're using an intermediate model you need to tell your relations to actually use that model. Add ->using(OrderAnnonce::class) to both of your relations:

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

More on Defining Custom Intermediate Table Models

brombeer
  • 8,716
  • 5
  • 21
  • 27