Php 如何在laravel 4.1中将模型作为参数传递?
所以,也许我没有很好地连接点,在我的模型之间。 我得到一个错误:Php 如何在laravel 4.1中将模型作为参数传递?,php,laravel,laravel-4,laravel-routing,Php,Laravel,Laravel 4,Laravel Routing,所以,也许我没有很好地连接点,在我的模型之间。 我得到一个错误: Argument 1 passed to RespuestaController::postRespuesta() must be an instance of Question, string given 在我看来,当我通过问题模型时: {{ Form::open( array( 'action' => array( 'RespuestaController@postRespuesta', $question->i
Argument 1 passed to RespuestaController::postRespuesta() must be an instance of Question, string given
在我看来,当我通过问题模型时:
{{ Form::open( array( 'action' => array( 'RespuestaController@postRespuesta', $question->id ) ) ) }}
以下是我的app/routes.php:
Route::model('respuesta', 'Respuesta');
Route::model('question', 'Question');
Route::resource('questions', 'QuestionController');
Route::controller('respuestas', 'RespuestaController');
和响应控制器:
class RespuestaController extends \BaseController {
public function postRespuesta(Question $question)
{
$validator = Validator::make(Input::all(),
array(
'respuesta' => 'required'
)
);
if( $validator->fails() ){
return Redirect::route('questions.show')
->withErrors($validator);
}else{
$respuesta = Input::get('respuesta');
$respuesta = Respuesta::create(array(
'respuesta' => $respuesta
)
);
$respuesta->save();
$question->respuesta()->associate($respuesta)->save();
$respuesta->user()->associate(Auth::user())->save();
//$question = Question::find($question); //this works without (Question $question), but i think is not a good practice or is it??
}
}
}
我做的模型绑定正确吗??或仅用于手动创建的管线,而不是仅用于
路由::控制器('respuestas','RespuestasController')
感谢您的支持。您需要将资源名称绑定为与模型资源相同的名称(而不是模型名称)-因此请尝试以下操作:
Route::model('questions', 'Question');
Route::resource('questions', 'QuestionController');
注意-不能在路由::控制器()
上使用此选项
我个人建议不要使用
resource()
或controller()
——而是手动定义所有路由。为什么你应该考虑手动定义你的所有路线。这证实了我的怀疑…无法将模型绑定到资源或restfull控制器。。。手动操作。。。再次感谢。。。