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

に置き忘れたようです$project。ここfind()にあるはず$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 に答える