user12332074
user12332074

Reputation:

Property does not exist on the Eloquent builder instance

I have an events table and a tickets table. I am trying to decrement the available tickets in regular_atendies by the ordered tickets and same for vip

This is my store method:

// Create Ticket

$ticket = new Ticket();
$ticket->userName = $request->input('userName');
$ticket->userEmail = $request->input('userEmail');
$ticket->phoneNumber = $request->input('phoneNumber');
$ticket->regular_quantity = $request->input('regular_quantity');
$ticket->vip_quantity = $request->input('vip_quantity');
$ticket->event_id = $request->route('id');
$ticket->total = $request->input('regular_quantity') + $request->input('vip_quantity');

$event = Event::find(1);
$event = Event::where('id',$ticket->event_id);

if ($ticket->regular_quantity<$event->regular_attendies) {
    DB::table('events')->decrement('regular_attendies', $ticket->regular_quantity);
} elseif ($ticket->vip_quantity<$event->$vip_attendies) {
    DB::table('events')->decrement('vip_attendies', $ticket->vip_quantity);  
} else {
    echo "error";
}

$ticket->save();

return redirect('/');

Upvotes: 2

Views: 9270

Answers (1)

Tim Lewis
Tim Lewis

Reputation: 29288

You never execute your query. This line:

$event = Event::where('id',$ticket->event_id);

Needs a closure:

$event = Event::where('id',$ticket->event_id)->first();
// or ->firstOrFail();, etc

Otherwise, it's a Builder instance, and you can't access columns from a Builder. Using ->first() will convert it to an Event instance, or null.

Also, having this:

$event = Event::find(1);
// ^ I assume this is used for testing, probably should comment it out.
$event = Event::where('id',$ticket->event_id)->first();

Is redundant. Use one or the other.

Upvotes: 7

Related Questions