nitz25's avatar

Dual declaration of models in one Controller

Hello, how can I pass the data of my two models in one controller? I have two Models: Hotel and Package What I want is to pass the record in one view using dropdown list..

This is my controller~

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Package;
use App\Hotel;

class DataController extends Controller
{
public function show_subhotel(){
        
        $packagedata = package::all();
        return view('hotel.package_sub_hotel', ['packagedata'=>$packagedata]);        
   }

public function show_subhotels(){
        $subhotel = Hotel::all();
        return view('hotel.package_sub_hotel', ['subhotel'=>$subhotel]); 
      }
}

And this is my view

  <div class="pure-control-group">
                
                    <label for="hotel">Hotel</label>

                        <select  id="hotel" class="drop" name="hotel">
                            
                            @foreach($subhotel as $subhotel)
                                <option value="{{$subhotel->hotel}}">{{$subhotel->hotel}}</option>
                            @endforeach
                       
                        </select>

                    </div>
 
                <div class="pure-control-group">
                
                    <label for="package">Package</label>

                        <select  id="package" class="drop" name="package">
                            
                            @foreach($packagedata as $packagedata)
                                <option value="{{$packagedata->package}}">{{$packagedata->package}}</option>
                            @endforeach
                          </select>
                        
                    </div> 

When I run the project it only read the first statement which is the $subhotel and UNDEFINED variable for $packagedata

Any help?

0 likes
1 reply
nitz25's avatar
nitz25
OP
Best Answer
Level 2

I got it!

public function show_subhotel(){
        
        $packagedata = package::all();
        $subhotel = Hotel::all();
        return view('hotel.package_sub_hotel', ['packagedata'=>$packagedata, 'subhotel'=>$subhotel]);        
   }

Please or to participate in this conversation.