1

我正在尝试更新我的记录ProjectsController,但是当我尝试路由到控制器时,我收到以下错误:

ErrorException
Undefined variable: project

我也不太确定我做错了什么,很抱歉让你们用代码超载,但不确定问题出在哪里。有点像 Laravel 的新手,所以很高兴能得到一些帮助!

它所指的功能如下:

public function edit($id)
{
    // get the project
    $project = Project::find($project);

    // show the edit form and pass the project
    return View::make('projects.edit')
        ->with('project', $project);

}

我的更新功能如下:

public function update($id)
{
    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
        'project_name'       => 'required',
        'project_brief'      => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if ($validator->fails()) {
        return Redirect::to('projects/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        // store
        $project = Project::find($id);
        $project->project_name = Input::get('project_name');
        $project->project_brief = Input::get('project_brief');
        $project->save();

        // redirect
        Session::flash('message', 'Successfully updated!');
        return Redirect::to('profile');
    }
}

我按如下方式路由到项目控制器:

Route::group(["before" => "auth"], function()
    {
    Route::any("project/create", [
        "as"   => "project/create",
        "uses" => "ProjectController@create"
    ]);
 Route::any("project/{resource}/edit", [
    "as"   => "project/edit",
    "uses" => "ProjectController@edit"
    ]);
Route::any("project/index", [
    "as"   => "project/index",
    "uses" => "ProjectController@index"
    ]);
    Route::any("project/store", [
    "as"   => "project/store",
    "uses" => "ProjectController@store"
    ]);
    Route::any("project/show", [
        "as"   => "project/show",
        "uses" => "ProjectController@show"
    ]);
});

我的表格如下:

<h1>Edit {{ $project->project_name }}</h1>

<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}

{{ Form::model($project, array('route' => array('projects.update', $project->id), 'method' => 'PUT')) }}

<div class="form-group">
    {{ Form::label('project_name', 'Project Name') }}
    {{ Form::text('project_name', null, array('class' => 'form-control')) }}
</div>

<div class="form-group">
    {{ Form::label('Project Brief', 'Project Brief') }}
    {{ Form::textarea('project_brief', null, array('class' => 'form-control', 'cols' => '100')) }}
</div>

{{ Form::submit('Edit the Project!', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}
4

1 回答 1

3

看起来你放错地方$projectfind(),应该在$id这里:

public function edit($id)
{
    // get the project
    $project = Project::find($id);

    // show the edit form and pass the project
    return View::make('projects.edit')
        ->with('project', $project);

}
于 2013-11-10T21:41:24.500 回答