1

I need to check if schedules_id equal to request schedules_id then need to check profile 'booked' or 'pending' then need to pass all the 'booked' profile seat values into blade.php. How can i do that or if another way or logic to do this please suggest me i'm new to laravel

how i getting data now :

class PassengersController extends Controller
{
    public function booking(Request $request)
    {
        //river is used to pass important params with flow of it from page to page
        $seat         = $request->seat;
        $buses_id     = $request->buses_id;
        $schedules_id = $request->schedules_id;
        $data         = Buses::where('buses_id', $buses_id)->first();
        $seat         = json_decode($data->seat_layout, true);
        $front        = json_decode($data->front_layout, true);
        $bookingSeat  = Bookings::whereColumn('schedules_id', 'schedules_id')->get();

        $bookingSeat = $bookingSeat->map(function ($bookSeat) {
            $bookSeat->seat = explode(",", $bookSeat->seat);
            return $bookSeat;
        });

        return view('frontend.booking', ['seat' => $seat, 'buses_id' => $buses_id, 'schedules_id' => $schedules_id, 'front' => $front, 'bookingSeet' => $bookingSeat]);

    }
}

blade.php

<div class="bus">
@foreach($seat as $key => $item)
@foreach($bookingSeet as $seer)
   <div class="col-md-1">
   <div class="seats back seats 
   @if(in_array($item['name'], $seer['seat']))
   activeSeat
   @endif"
   data-id="{{$key}}">
   <div class="special-attributes"></div>
   @if(isset($item['name'])){{$item['name']}}@else 11A @endif

   <input type="checkbox" name="seat_id[]" id="{{$key}}" value="{{$key}}">

   </div>
   </div>
@endforeach
@endforeach
</div>

table

bookings_id users_id schedules_id buses_id routes_id seat price profile
    1           1         6           1       3        1  Null  pending
    2           1         6           1       3        2  Null  booked
    3           1         6           1       3        3  null  booked

Problem with my current code is getting 2 array with all bookings table column how to pass that value into blade, when i try i getting duplicated checkbox for ex. one bus have a 50 seat but now showing 100 seat. seats like 1,1,2,2 in view

One array look like this :

Collection {#426 ▼
  #items: array:2 [▼
    0 => Bookings {#431 ▼
      #fillable: array:7 [▶]
      #primaryKey: "bookings_id"
      #connection: "mysql"
      #table: null
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▼
        "bookings_id" => 1
        "users_id" => 1
        "schedules_id" => 6
        "buses_id" => 1
        "routes_id" => 3
        "seat" => "1"
        "price" => null
        "profile" => "pending"
        "created_at" => "2019-04-09 00:00:00"
        "updated_at" => "2019-04-09 00:00:00"
      ]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
    1 => Bookings {#432 ▶}
  ]
} 
thush
  • 117
  • 9

4 Answers4

0

Try this:

$bookingSeat  = Bookings::where('schedules_id', $request->schedules_id)->where(function ($q) {
    $q->where('profile', 'booked')->orWhere('profile', 'pending');
})->get();

or this:

$profileTypes = ['booked', 'pending'];
$bookingSeat  = Bookings::where('schedules_id', $request->schedules_id)->whereIn('profile', $profileTypes)->get();

@Edit

In this way, if your Bookings table contains all seats, already registered (1 to 50), it will return all seats that your profile is "booked" or "pending" for a particular bus, route, and time.

$profileTypes = ['booked', 'pending'];
$bookingSeat  = Bookings::where('schedules_id', $request->schedules_id)
    ->whereIn('profile', $profileTypes)
    ->where('buses_id', $buses_id)
    ->where('routes_id', $request->route_id)
    ->get();
HerickC
  • 344
  • 1
  • 10
  • but getting 2 array with all bookings table column how to pass that value into blade, when i try i getting duplicated checkbox for ex. one bus have a 50 seat but now showing 100 seat. seats like 1,1,2,2 in view – thush Apr 15 '19 at 12:30
  • Probably you have two bus, this query don't check the `buses_id`, maybe you has to limit this... – HerickC Apr 15 '19 at 12:37
  • but both bus id's are same, how can i limit that sir? – thush Apr 15 '19 at 12:41
  • Because of the logic of your table, if there are more than one bus with the same ID, then need to change either the routes_id, or schedules_id. All this must be passed in Query, so that a user can choose exactly the right bus, in the right destination, at the right time. I will edit the answer to contemplate this. – HerickC Apr 15 '19 at 12:47
  • need any changes in blade.php sir? or i can use my old one? – thush Apr 15 '19 at 12:55
0

try this

Bookings::where('schedules_id', $schedules_id)->where('profile', 'booked')->get();
guruprasad ks
  • 312
  • 1
  • 7
  • but getting 2 array with all bookings table column how to pass that value into blade, when i try i getting duplicated checkbox for ex. one bus have a 50 seat but now showing 100 seat. seats like 1,1,2,2 in view – thush Apr 15 '19 at 12:29
  • can you show me your foreach loop in blade file and also send the output of the above – guruprasad ks Apr 15 '19 at 12:33
  • remove first foreach loop and try only with second loop. it is foreach loop issue after first loop it is looping again the same $bookingseet – guruprasad ks Apr 15 '19 at 12:45
  • 1
    i cant remove that bcz im checking condition with first foreach you can see that in blade. first loop have a seat layout coming from bus table then check with second loop if it is book or not – thush Apr 15 '19 at 12:51
  • add that condition in the beginning of the second loop and then try – guruprasad ks Apr 15 '19 at 12:55
  • got error bcz im using foreach name in condition sir my condition"@if(in_array($item['name'], $seer['seat'])) activeSeat @endif"" – thush Apr 15 '19 at 13:06
  • sorry for troubling you – thush Apr 15 '19 at 13:08
  • @foreach($bookingSeet as $seer) @if(in_array($item['name'], $seer['seat'])) .... @endif endforeach – guruprasad ks Apr 15 '19 at 13:08
  • but how to call activeSeat css class u forgot that in ur condition sir? – thush Apr 15 '19 at 13:16
  • @thush you can directly call the put activeSeat in your div as it is satisfying first only – guruprasad ks Apr 16 '19 at 06:24
  • @guruprasadks i tried your all suggestion but didn't worked for me again im getting duplicated seats – thush Apr 16 '19 at 13:05
0

You are allowed to use arrays als wheres.

For example:

$model->where([
      ['field-one', '=', 'valueOne'],
      ['field-two', '=', 'valueY']
]);
Chiel Bos
  • 1
  • 2
0

If I understand the situation correctly, if schedules_id is provided schedules_id and the profile is either booked or pending, you want to retrieve data, the query should be

Bookings::where('schedules_id', $schedules_id)->whereIn('profile', ['booked', 'pending'])->get();
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105