best way to send an order mailable
From many examples I have found on the web, it's best to use the user_id in an orders table and utilize a pivot table. e.g. for the contents of the order itself.
If you have events in your app, say JobPosted, BidReservePlaced these two fire an event that is associated with an order, money is charged, and hopefully a mailable sent to document the order.
How would you structure the order mailable? There are many possible items that could be in an order.
class CreateOrdersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('confirmation_number');
$table->integer('amount');
$table->string('card_last_four');
$table->timestamps();
});
}
Below is a pivot table.
class CreateBidReservesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('bidreserves', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('job_id');
$table->unsignedBigInteger('order_id')->nullable();
$table->foreign('job_id')->references('id')->on('jobs');
$table->bigInteger('amount');
$table->timestamps();
});
}
Should the order have many events dispatched? Or should only Job or BidReserve have the dispatch?
in short, who should tell whom what to do?
class Order extends Model
{
....
protected $dispatchesEvents = [
'created' => BidReservePlaced::class
];
protected $dispatchesEvents = [
'created' => JobPosted::class
];
....
I'm basing this strategy on the video: https://laracasts.com/series/laravel-from-scratch-2018/episodes/32
another thought I've had is to send the order mail in the controller, after a successful charge.
Please or to participate in this conversation.