从其他函数LARAVEL访问变量

时间:2017-04-07 09:55:06

标签: laravel variables controller access

我正在尝试将函数的返回结果用于另一个函数。这是我的第一个功能:

public function availableRooms(){
    if(isset($_POST['selectedDate'])){
        $checkInDateReceived = str_replace('/', '-', $_POST['selectedDate']);
        $checkInDate = date('Y-m-d', strtotime($checkInDateReceived));
        $availableRooms = DB::table('rooms')
            ->leftJoin('reservations', function ($secondJoin) use($checkInDate) {
                $secondJoin->where('reservations.checkout_date', '<', $checkInDate);
            })
            ->leftJoin('reservation_room_rel', function ($firstJoin) {
                $firstJoin
                    ->on('reservation_room_rel.room_id', '=', 'rooms.id')
                    ->on('reservation_room_rel.reservation_id', '=', 'reservations.id');
            })
            ->where([
                ['rooms.status', '=', 1]
            ])
            ->whereNull('reservation_room_rel.room_id')
            ->get();
        return $availableRooms;
    }
}

这是我的第二个功能:

public function returnAddReservation(){
    $availableRoomsRet = $this->availableRooms();
    $mealSupplement = new Meal;
    $accommodationSupplement = new AccommodationSupplement;

    $accommodationSupplements = $accommodationSupplement::where('status', 1)
        ->orderBy('name', 'asc')
        ->get();
    $mealSupplements = $mealSupplement::where('status', 1)
        ->orderBy('name', 'asc')
        ->get();
    return view('addReservation', [
        'availableRooms' => $availableRoomsRet,
        'accommodationSupplements' => $accommodationSupplements,
        'mealSupplements' => $mealSupplements
    ]);
}

如果第一个函数独立运行,则返回正确的结果,但是当我dd($availableRoomsRet)时,它返回null。 知道如何从$availableRooms正确访问public function availableRooms()public function returnAddReservation()吗? 谢谢!

0 个答案:

没有答案
相关问题