diff --git a/app/Concert.php b/app/Concert.php index f7ed0f2..f92d6ff 100644 --- a/app/Concert.php +++ b/app/Concert.php @@ -32,7 +32,7 @@ class Concert extends Model public function orders() { - return $this->hasMany(Order::class); + return $this->belongsToMany(Order::class, 'tickets'); } public function hasOrderFor($customerEmail) @@ -69,9 +69,9 @@ class Concert extends Model public function createOrder($email, $tickets) { - $order = $this->orders()->create([ + $order = Order::create([ 'email' => $email, - 'amount' => $tickets->count() * $this->ticket_price, + 'amount' => $tickets->sum('price'), ]); foreach ($tickets as $ticket) { diff --git a/app/Ticket.php b/app/Ticket.php index dafd0ff..584872d 100644 --- a/app/Ticket.php +++ b/app/Ticket.php @@ -17,4 +17,14 @@ class Ticket extends Model { $this->update(['order_id' => null]); } + + public function concert() + { + return $this->belongsTo(Concert::class); + } + + public function getPriceAttribute() + { + return $this->concert->ticket_price; + } } diff --git a/database/migrations/2016_11_08_205823_create_orders_table.php b/database/migrations/2016_11_08_205823_create_orders_table.php index af6bd96..850a6be 100644 --- a/database/migrations/2016_11_08_205823_create_orders_table.php +++ b/database/migrations/2016_11_08_205823_create_orders_table.php @@ -15,7 +15,6 @@ class CreateOrdersTable extends Migration { Schema::create('orders', function (Blueprint $table) { $table->increments('id'); - $table->unsignedInteger('concert_id'); $table->integer('amount'); $table->string('email'); $table->timestamps(); diff --git a/tests/features/PurchaseTicketsTest.php b/tests/features/PurchaseTicketsTest.php index 983770e..61a3bc5 100644 --- a/tests/features/PurchaseTicketsTest.php +++ b/tests/features/PurchaseTicketsTest.php @@ -32,6 +32,7 @@ class PurchaseTicketsTest extends TestCase /** @test */ function customer_can_purchase_tickets_to_a_published_concert() { + $this->disableExceptionHandling(); $concert = factory(Concert::class)->states('published')->create(['ticket_price' => 3250])->addTickets(3); $this->orderTickets($concert, [