无法执行UPDATE操作 - 编辑项目

时间:2016-09-22 16:33:26

标签: php laravel

我无法在我的控制器中编辑我这样创建的项目:

  public function newProject(Request $request)
    {
        $data = $request->all();

        $attributes = [];
        $attributes['title'] = $data['title'];
        $attributes['start_date'] = date("Y-m-d h:i:s", strtotime($data['start_date']));
        $attributes['end_date'] = date("Y-m-d h:i:s", strtotime($data['end_date']));
        $attributes['created_by'] = Auth::user()->id;
        $attributes['description'] = $data['description'];
        $attributes['air'] = '10';
        $attributes['water'] = '19';
        $attributes['lat'] = $data['lat'];
        $attributes['lng'] = $data['lng'];


//        var_dump($attributes);
//        return;

        $project = Projects::create($attributes);

        if($project)
            return redirect('home')->with('success', 'Project added successfully');

        var_dump($data);
        return;
    }

现在,我想编辑我使用上述功能创建的项目。我尝试了相同的程序只是改变路线:

Route::post('projects/new', [
    'uses' => 'ProjectsController@newProject',
    'as' => 'projects.new',
]);

注意:这是我创建项目的功能和路线。有什么提示如何编辑?

更新:

我的刀片模板文件

@extends('layouts.app')

@section('content')
    <div class="container">

        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">Add Project</div>


                    <div class="panel-body">
                        <form class="form-horizontal" role="form" method="POST" action="{{ route('projects.update') }}">
                            {{ csrf_field() }}

                            <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                                <label for="title" class="col-md-4 control-label">Title</label>

                                <div class="col-md-7">
                                    <input id="title" type="text" class="form-control" name="title" value="{{ $project->title }}">

                                    @if ($errors->has('title'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('title') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
                                <label for="email" class="col-md-4 control-label">Start Date</label>

                                <div class="col-md-7">
                                    <input id="date" type="date" class="form-control" name="start_date" value="{{ $project->start_date }}">

                                    @if ($errors->has('date'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('date') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group{{ $errors->has('date') ? ' has-error' : '' }}">
                                <label for="date" class="col-md-4 control-label">End Date</label>

                                <div class="col-md-7">
                                    <input id="date" type="date" class="form-control" name="end_date" value="{{ $project->end_date }}">

                                    @if ($errors->has('date '))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('date') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>


                            <div class="form-group{{ $errors->has('text') ? ' has-error' : '' }}">
                                <label for="email" class="col-md-4 control-label">Description</label>
                                <div class="col-md-7">
                                    <textarea class="form-control" rows="3" name="description" >{{ $project->description }} </textarea>

                                    @if ($errors->has('email'))
                                        <span class="help-block">
                                        <strong>{{ $errors->first('textarea') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <label class="col-md-7 control-label"></label>

                            <span>
                                <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample" >
                                    Air
                                </a>
                            </span>
                            <span>
                                <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample2" aria-expanded="false" aria-controls="collapseExample2">
                                    Water
                                </a>
                            </span>

                            <span>
                                <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample3" aria-expanded="false" aria-controls="collapseExample">
                                    Location
                                </a>
                            </span>



                            <div class="collapse" id="collapseExample">
                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-4 control-label">Temperature</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="temperature" class="form-control" placeholder=" °C" value="{{$project->temperature}}">
                                        </div>


                                    </div>
                                </div>




                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-4 control-label">Radiation</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="radiation" class="form-control" placeholder=".col-xs-1" vlaue="{{$project->radiation}}">
                                        </div>


                                    </div>
                                </div>




                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-4 control-label">Dust</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="dust" class="form-control" placeholder=".col-xs-1" value="{{$project->dust}}">
                                        </div>


                                    </div>
                                </div>


                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-4 control-label">Noise</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="noise" class="form-control" placeholder=".col-xs-1" value="{{$project->noise}}">
                                        </div>


                                    </div>
                                </div>

                            </div>

                            <p> </p>

                            <div class="collapse" id="collapseExample2">
                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Natrium</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="natrium" class="form-control" placeholder=".col-xs-1" value="{{$project->natrium}}">
                                        </div>


                                    </div>
                                </div>




                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Phosporus</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="phosporus" class="form-control" placeholder=".col-xs-1" value="{{$project->phosphorus}}">
                                        </div>


                                    </div>
                                </div>



                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Arsinic</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="arsinic" class="form-control" placeholder=".col-xs-1" value="{{$project->arsinic}}">
                                        </div>


                                    </div>
                                </div>



                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Barium</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="barium" class="form-control" placeholder=".col-xs-1" value="{{$project->barium}}">
                                        </div>


                                    </div>
                                </div>


                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Cadmium</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="cadmium" class="form-control" placeholder=".col-xs-1" value="{{$project->cadium}}">
                                        </div>


                                    </div>
                                </div>


                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Mercury</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="mercury    " class="form-control" placeholder=".col-xs-1" value="{{$project->mercury}}">
                                        </div>


                                    </div>
                                </div>


                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Selenium</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="selenium" class="form-control" placeholder=".col-xs-1" value="{{$project->selenium}}">
                                        </div>


                                    </div>
                                </div>



                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Nickel </label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="nickel" class="form-control" placeholder=".col-xs-1" value="{{$project->nickel}}">
                                        </div>


                                    </div>
                                </div>



                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Thallium</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="thallium" class="form-control" placeholder=".col-xs-1" value="{{$project->thallium}}">
                                        </div>


                                    </div>
                                </div>




                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Berillyum</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="berillyum" class="form-control" placeholder=".col-xs-1" value="{{$project->berillyum}}">
                                        </div>


                                    </div>
                                </div>




                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Phenols</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="phenols" class="form-control" placeholder=".col-xs-1" value="{{$project->phenols}}">
                                        </div>


                                    </div>
                                </div>



                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Oil</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="oil" class="form-control" placeholder=".col-xs-1" value="{{$project->oil}}">
                                        </div>


                                    </div>
                                </div>

                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Temperature</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="w_temperature" class="form-control" placeholder="°C" value="{{$project->w_temperature}}">
                                        </div>


                                    </div>
                                </div>


                                <div class="form-group{{ $errors->has('number') ? ' has-error' : '' }}">
                                    <label for="text" class="col-md-7 control-label">Oxygen</label>
                                    <div class="row">
                                        <div class="col-xs-1">
                                            <input type="text" name="oxygen" class="form-control" placeholder="mg/L" value="{{$project->oxygen}}">
                                        </div>


                                    </div>
                                </div>


                            </div>



                            <label for="text" class="col-md-4 control-label">Location</label>
                            <div class="row">
                                <div class="col-md-7">

                                    <h1>Location</h1>
                                    <p></p>

                                    <!--map div-->
                                    <div id="map"></div>


                                    <!--our form-->
                                    <h2 class="text-left">Coordinates</h2>
                                    <p>
                                        <input class="form-control"  type="text" id="lat" name="lat" value="{{$project->lat}}" />
                                        <input class="form-control"  type="text" id="lng" name="lng" value="{{$project->lng}}"/>
                                    </p>


                                </div>


                            </div>



                            <div class="form-group">
                                <div class="col-md-7 col-md-offset-4">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-btn fa-user"></i> Add Project
                                    </button>
                                </div>
                            </div>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </div>



    </div>

@endsection
@section('footer')

@endsection

2 个答案:

答案 0 :(得分:1)

我建议采用简单方法

控制器功能

public function update($id, Request $request)
{
    $data = $request->all();

    $project = Projects::findOrFail($id);
    $project->update($data);

    return redirect(/home);

}

<强>路线

Route::patch('projects/{id}', [  // this is the project id 
    'uses' => 'ProjectsController@update',
    'as' => 'projects.update',
]);

表格(假设您使用的是laravel集体表格)

{!! Form::model($project, [
    'method' => 'PATCH',
    'url' => ['/project', $project->id], // this is where you submit the id of the project 
    'class' => 'form-horizontal',
    'files' => true
]) !!}

                <div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
            {!! Form::label('name', 'Name', ['class' => 'col-sm-3 control-label']) !!}
            <div class="col-sm-6">
                {!! Form::text('name', null, ['class' => 'form-control']) !!}
                {!! $errors->first('name', '<p class="help-block">:message</p>') !!}
            </div>
        </div>


    <div class="form-group">
        <div class="col-sm-offset-3 col-sm-3">
            {!! Form::submit('Update', ['class' => 'btn btn-primary form-control']) !!}
        </div>
    </div>
{!! Form::close() !!}

这是最好的方式!我不建议使用相同的功能,除非你确定你想要的。

<强>更新

 <form class="form-horizontal" role="form" method="POST" action="{{ url('projects', $project->id) }}"> 

    {{ csrf_field() }}
 <input name="_method" type="hidden" value="PATCH"> // add this
// here you put the rest of the  form

</form>

如果您没有此$project变量可用      请告诉我显示编辑页面的功能      因为我需要知道你将如何传递你想要更新的项目的ID(用户输入与否)

答案 1 :(得分:0)

您可以在一个功能中完成所有操作:

public function storeOrUpdate(Request $request) {
    try{ 
        DB::transaction(function() use($request) {
            Projects::firstOrNew([
                'id' => $request->has('id') ? $request->get('id') : null
                'title' => $request->get('title'),
                'start_date' => $request->has('start_date') ? Carbon\Carbon::parse($request->get('start_date')) ? null,
                'end_date' => $request->has('end_date') ? Carbon\Carbon::parse($request->get('end_date')) ? null,
                'created_by' => auth()->user()->id,
                'description' => $request->get('description'),
                'air' => 19,
                'water' => 10,
                'lat' => $request->get('lat'),
                'lng' => $request->get('lng')
            ]);
        });

        DB::commit();
        return redirect('home')->with('Success', 'Project added successfully');
    } catch (\Exception $e){
        DB::rollback();

        return redirect()->back()->withErrors(collect($e->getMessage())->toArray);
    }
});