csow
csow

Reputation: 47

Laravel select where/count query

There are two Mysql tables:


1.reservations(id,screening_id,reserved). the reserved is a boolean its value "1".


  1. table seat_reserveds(id,screening_id,reservation_id,seat_id).

I want the following: 1.get the reservation id select id from reservation where screening_id = id and reserved = 1 In laravel looks like:

          $reservation_id = DB::table('reservations')->select('id')- 
          >where('screening_id',$id)->where('reserved','1')->get();

Then I want to count how many seats are reserved


Select count(id) from seat_resrveds where where screening_id = id and reservtain_id = id. In laravel looks like:

        $reservedtickets = DB::table('seat_reseverds')- 
       >where('screening_id',$id)->where('reservation_id',$reservation_id)->count('id');

I think the problam is that I get "id":number from $reservation_id so cant use it at the $reservedtitckets query because I only need a number. How can I write these querys.

Upvotes: 0

Views: 523

Answers (2)

Arun Code
Arun Code

Reputation: 1638

Update the query to the following.

    $reservation = DB::table('reservations')
      ->where('screening_id',$id)
      ->where('reserved', 1)
      ->first();

Then the $reservation_id should be as below.

$reservation_id = $reservation->id;

You can pass this parameter to your next query for $reservedtickets.

Upvotes: 0

Paul Spiegel
Paul Spiegel

Reputation: 31772

Use ->value('id') to get a single value from a query:

$reservation_id = DB::table('reservations')
    ->where('screening_id',$id)
    ->where('reserved','1')
    ->value('id');

Upvotes: 1

Related Questions