【发布时间】:2014-03-28 11:26:44
【问题描述】:
routes.php
Route::post("/{user}/{char_name}", array(
'as' => 'char-profile-post',
'uses' => 'ProfileController@postDropDownList'));
Route::get("/{user}/{char_name}", array(
'as' => 'char-profile-get',
'uses' => 'ProfileController@getDropDownList'));
ProfileController.php
public function postDropDownList($user, $char_name) {
$user = Auth::user();
$char_name = Input::get('choose');
$char_name_obj = User::find($user->id)->characters()->where('char_name', '=', $char_name)->first();
return Redirect::route('char-profile-get', array(Session::get('theuser'), $char_name_obj->char_name));
}
public function getDropDownList($user, $char_name) {
return View::make('layout.profile')
->with('char_name', $char_name);
}
layout.profile 视图中的片段
<form action="{{ URL::route('char-profile-post') }}" method="post">
<select name="choose">
<option value="choose">Choose a character</option>
@foreach($char_name as $c)
<option> {{ $c->char_name }} </option>
@endforeach
</select>
<input type="submit" value="Ok">
{{ Form::token() }}
</form>
错误异常
未定义变量:char_name
(查看:C:\wamp\www\CaughtMiddle\tutorial\app\views\layout\profile.blade.php)
在编写以下内容时,我仍然收到此错误。
<option> {{ $char_name['char_name'] }} </option>
我的问题很明显。是我做错了什么,还是 Laravel 真的无法将几个变量从控制器发送到视图?
【问题讨论】:
-
你的意思是把它放在这里?公共函数 getDropDownList($user, $char_name) { var_dump($char_name);返回视图::make('layout.profile') ->with('char_name', $char_name); } 它什么也不做。同样的错误,没有其他消息。
-
我已经从视图中删除了下拉列表,并按照你告诉我的做了。该变量似乎为空。它不会在浏览器上写任何东西。但为什么它是空的?
-
这个变量来自您的第二个 URL 参数。这就是你在路线中写的:
Route::get("/{user}/{char_name}", array(。你在 URL 中传递了什么?您是否发布了完整的getDropDownList功能?你想完成什么? -
是的。我有一个查询数据库的 post 路由和一个尝试将新值带到视图的 get 路由。首先,我想用数据库中的值填充一个下拉列表。其次,我想获取选定的值并将其写入视图中。类似的东西。您选择了 {{$char_name}}。这背后的想法是我将尝试将 $char_name 附加到新 URL。我需要这样做,因为我需要那个变量。我知道如何填充下拉列表,但该解决方案不适合我正在尝试做的事情。
-
我总是试图绕过我现在正在做的事情,直接从视图中查询数据库等等,但仅此而已。这真的很令人沮丧,因为由于这个问题我无法组织我的代码。我可以给你一个有效的例子,我不知道为什么。
标签: laravel controller blade