简体   繁体   中英

Laravel problem with passing variables from controller to view

I am working on a laravel project and having a problem passing variables from the controller to the view:

this is the controller method:

public function index($id){
        $data = [];
        $company = DB::table('companies')->where('id',$id)->first();
        return view('searchResultAdmin.index',  ['company'=>$company, 'Data'=>$data]);
    }

and this is the view :

{{ Form::open(array('url' => '/SearchResultAdmin',  'method' => 'post')) }}
                            <div class="radio-buttons display-inline">
                                <fieldset class="display-inline group1">
                                    <label class="radio-inline">
                                        <input type="radio" name="SearchLocation" id="value1" value="Actuals" checked>Actuals
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="SearchLocation" id="value2" value="Forecasts">Forecasts
                                    </label>
                                </fieldset>
                                {{ Form::hidden('id', ($company->id)) }}
                                <fieldset class="display-inline">
                                    <label class="radio-inline">
                                        <input type="radio" name="Order" id="value3" value="Ascending" checked>Ascending
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="Order" id="value4" value="Descending">Descending
                                    </label>
                                </fieldset>
                            </div>
                            <div class="dropdown-header-table display-inline">
                                <h5>Search By:
                                    <select name="SearchBy">
                                        <option value="Account">Account</option>
                                        <option value="Name">Name</option>
                                        <option value="Year">Year</option>
                                        <option value="Description">Description</option>
                                        <option value="Amount">Amount</option>
                                    </select>
                                </h5>
                            </div>
                            <div class="search-input display-inline">
                                <div class="table-form-header">
                                    <input type="text" class="form-control" name="SearchInputs" placeholder="Search...">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="submit">
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </span>
                                </div>
                            </div>
                        {{ Form::close() }}

the error says: undefined variable company on view

You get the error because of

{{ Form::hidden('id', ($company->id)) }}

Add dd($company) or dd($company->id) in your controller before the returning the view and see what you get.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM