2014-02-20 32 views
1

我試圖爲此獲得一個解決方案几個小時,我真的不明白爲什麼它不工作。在Laravel視圖中的未定義變量

routes.php文件

Route::get("/user/Session::get('theuser')/charname", array(
    'as' => 'profile-character', 
    'uses' => '[email protected]' 
    )); 

ProfileController.php

public function dropDownList() { 

$list = Character::lists('char_name', 'char_dynasty'); 

return View::make('layout.profile')->with('character_options',$list); 

} 

profile.blade.php

<div class="selected_char"> 

<form action="{{ URL::route('profile-character') }}" method="get"> 

{{ Form::select('character', $character_options , Input::old('character')) }} 


</form> 
</div> 

此代碼拋出我的錯誤是,$ character_options是不確定的。我已經閱讀了幾十個例子,像我一樣爲他們工作。也許這是Laravel 3?

+0

您能得到什麼,如果在'下拉列表( )'你做'dd(Character :: lists('char_name','char_dynasty'));'? –

+0

相同的錯誤未定義的變量:character_options –

+0

如果出現該錯誤,則表示控制器方法未被執行,因爲控制器方法中的dd()甚至不允許呈現視圖。因此,也許路由不能按預期工作(這確實解釋了Session :: get()如何在你的路由定義中起作用(它不是!),而是其他一些路由正在打開哪些調用那個配置文件視圖而沒有設置視圖中的'character_options'變量?在命令行上執行'php artisan routes'並查看Laravel *認爲*您的路線是什麼。您可能會感到驚訝! – alexrussell

回答

0

這是一件不應該Laravel工作:

Route::get("/user/Session::get('theuser')/charname", array(
    'as' => 'profile-character', 
    'uses' => '[email protected]' 
    )); 

你不能在你的路線就像Session::get()打電話,不是隻要我至少知道。你的路由將起作用,因爲它是一個對路由有效的字符串,但Session :: get()永遠不會真的在這個過程中被使用。

如果你從字面上打網址:

http://localhost/user/Session::get('theuser')/charname 

你能擁有的是一樣的東西:

Route::get("/user/{user}/charname", array(
    'as' => 'profile-character', 
    'uses' => '[email protected]' 
    )); 

,做

public function dropDownList($user) { 

    $list = Character::lists('char_name', 'char_dynasty'); 

    return View::make('layout.profile')->with('character_options',$list); 

} 
+0

Route :: get(「/ user/Session :: get('theuser')/ charname「,array( 'as'=>'profile-character', 'uses'=>'ProfileController @ dropDownList' )); working。我無法解釋我嘗試了使用公共函數dropDownList($ user)public function dropDownList($ character_options)和public function dropDownList($ list)。我設法使用{{Form :: select ('character',Character :: where('user_id','=',Auth :: User() - > id) - > lists('char_name','char_dynasty'),Input :: old('character'))}}這是一個可行的解決方案嗎? –

+0

編輯爲了告訴你它爲什麼有效,但它確實沒有。你應該用這段代碼解釋你想要完成的事情,這樣人們就會更好地理解它,並試圖幫助你。 –

+0

我想填充一個下拉列表與我的數據庫中的數據。我想在我的Controller中編寫一個函數,然後將結果返回給我的View。我想我不能這樣做,但直接從視圖查詢我的數據庫。 –